language icon indicating copy to clipboard operation
language copied to clipboard

Optional Semicolons

Open mit-mit opened this issue 7 years ago • 180 comments
trafficstars

Many programming languages (incl. TypeScript, Swift, and Kotlin) support omitting semicolons as explicit statement separators. We consistently hear from developers who come to Dart from those languages (for example, to implement Flutter apps), that they find the mandatory semicolons irritating & pointless. In addition, removing the need for terminating statements with semicolons would also support our overall goal of reducing boilerplate, and making Dart as terse as possible; however we need to be mindful of also keeping it readable, unambiguous, and unsurprising.

Current status: After a longer investigation we have currently put this on hold, as a reasonable implementation of this would have to involve some rather large syntax changes to the language. Please see the detailed write-up for additional information.

mit-mit avatar Oct 31 '18 14:10 mit-mit

I love the idea of optional semicolons :+1:

However, I think this issue is missing a problem statement. Pointing to other languages is useful, but those are solutions to a problem. They do not describe the problem.

yjbanov avatar Oct 31 '18 20:10 yjbanov

Updated the description. This will be covered in much more detail in the actual proposal.

mit-mit avatar Nov 01 '18 08:11 mit-mit

Related SDK issues: https://github.com/dart-lang/sdk/issues/34, https://github.com/dart-lang/sdk/issues/30347

mit-mit avatar Nov 01 '18 08:11 mit-mit

Maybe there could be additional syntax to avoid ambiguity, only in cases where it could lead to ambiguities, something like:

code ---
code;

Or

;;code 
  code;

feinstein avatar Nov 16 '18 14:11 feinstein

Definitely, there are always ways to avoid the ambiguity. Parenthesizing an expression or removing a newline is usually sufficient.

The issue is more around whether the user finds those ambiguities intuitive and considers the workarounds reasonable. (JavaScript is a good example of a language that has workarounds for its newline ambiguities, but those workarounds are so confusing and onerous that most users just use ; instead.)

munificent avatar Nov 28 '18 20:11 munificent

I agree, this should be a feature

egdelgadillo avatar Dec 04 '18 14:12 egdelgadillo

Optional semicolons is the first step to fuck up the code style if you are not only one developer on the project. I saw a lot of JavaScript projects and it feels like scratching your eyes, when there is semicolon on one string, and there is no on the next similar.

korgara avatar Jan 12 '19 10:01 korgara

Well, it can then be project wide toggable, Could be an option in one of the project config files, so all the code stays even. This would mean that if it's deactivated, using semi-colon to end a line should raise an exeption too, same for the opposite if semi-colon is activated in the current project

egdelgadillo avatar Jan 14 '19 11:01 egdelgadillo

I saw a lot of JavaScript projects and it feels like scratching your eyes, when there is semicolon on one string, and there is no on the next similar.

We have already have an automated code formatter, dartfmt, that is in wide use. If we do optional semicolons, dartfmt will remove them and the code will stay consistent.

munificent avatar Jan 14 '19 17:01 munificent

When I got in touch with dart at two years ago, I always have a question about the semicolons for two years while using dart for coding.

In Golang and many other languages, the semicolons are optional and I think it's a good idea. The mandatory semicolons at end of each line are really redundant in dart. And I think if in some conditions the semicolons are essential, we maybe could find a replacement. Such as in an abstract class in dart:

abstract class Animal {
  void cry(); // cry is an abstract method because of absence of method body and the end semicolon.
}

It can be written as:

abstract class Animal {
  abstract  void cry()  // omit the end semicolon by using 'abstract' as a replacement.
}

If the semicolons at the end of every line could be omitted, it will be more effective and more terse than now. And the IDE will not report an error again when writing a new line that is uncompleted with a semicolon.

I really hope the semicolons could be optional at the future sdk versions.

Buerkut avatar Jan 29 '19 01:01 Buerkut

I find the semicolons in Dart extremely tedious (as a developer coming from TypeScript, Swift, Kotlin and other languages), in fact in first few days with Dart about 50% of failures to compile turned out to be a missing semicolon.

While I would love if if Dart made them optional, perhaps dartfmt could insert them in 'obvious' cases? I imagine this is somewhat hard, but Prettier does it reliably (to the extent where I don't mind semicolons in TypeScript/Javascript as Prettier writes them for me).

jamesporter avatar Feb 03 '19 22:02 jamesporter

Two things i want to mention that would make code Dart likely fun

  1. Remove the semicolons :- All new langauges have removed semicolon . coding dart reminds me of java days. For readibility mordern code editors automically format code .

  2. Convert switch statement to when like in kotlin its more readable and take less code write.

nayan2823 avatar Mar 10 '19 05:03 nayan2823

I did a bunch more investigation of optional semicolons after writing the "Terminating Tokens" proposal. I wrote up my thoughts on the experience here.

munificent avatar Mar 11 '19 17:03 munificent

I started learning Dart this week and was quite surprised to know that it requires semicolons. In all modern languages like Kotlin, Swift and Go, you can omit them. I thought these days of old verbose programming were over...

rodolfoggp avatar Mar 15 '19 12:03 rodolfoggp

In almost 2 years of working with Kotlin (nearly full time) I haven't had problems because of semicolons a single time. On the other side, in less than a month of working with Dart, I had already 2-3 occasions where I spent a while fixing compiler errors because of them. So at least based on my own experience, they seem unnecessary and rather a hindrance. And ugly, of course.

ivnsch avatar Mar 17 '19 19:03 ivnsch

Oh. Right. You're saying that you suspect that some Kotlin expressions are weird because of its optional semicolons. I also suspect many things, but wouldn't bring that up in an argument with nothing to back it up.

Also, considering your (completely subjective) negativity towards Kotlin, as could be read in the deleted conversation, it's most likely pointless to argue about this "weirdness".

And why focus on Kotlin? Plenty of other langs seem to be doing quite well without semicolons, including Haskell, Scala, F#, Typescript, Ruby, Swift and matlab, among others. Are those "weird" too?

ivnsch avatar Mar 17 '19 19:03 ivnsch

Thanks for taking it offline and cleaning up. I realize feelings run high on some of the issues we discuss, but we really need to keep the discussions here technical and professional if we're going to continue to use this as a primary venue for doing design work.

leafpetersen avatar Mar 18 '19 18:03 leafpetersen

There are no semicolons in almost all modern languages, and dart's semicolons surprise me.

I'm glad to see this vote. It's clear that there are few opponents.

yisar avatar Mar 20 '19 03:03 yisar

It's clear that there are few opponents.

Yes, I think most users want us to eliminate the semicolons. The hard point is designing a method for how to do it, and so far we haven't been able to come up with a set of grammar changes simple enough to feel confident in shipping it.

munificent avatar Mar 20 '19 21:03 munificent

According to our Q1 survey, about 40% of our users think removing semicolons is worth the cost of a breaking change, 41% if there was a way to do it automatically. Contrast to 47% who think non-nullable types are worth it (51% if it could be automated), 55% who think real Unicode support in strings is worth it (56% with automation), 47% who think renaming types to make them consistent is worth it (55% with automation), and 16% who think that no change could be worth breaking backwards compatibility (13% think no change could be worth breaking backwards compatibility even with tooling).

So at least as far as Flutter goes, it's not the case that most of our users would rather we did this.

Hixie avatar Mar 21 '19 03:03 Hixie

Is there any Dart feature where there was a substantial majority (above 60%)? From this data, it sounds like people just don't like breaking changes. In which case 40% would still be relatively high.

A more to the point question would be simply whether we prefer semicolons or no semicolons. It's then up to the results of the other features, the cost of implementing the change, the available capacity, etc. to decide if and when it will be done.

FWIW to me personally this shouldn't be high in the priority list - it's a cosmetic improvement, that doesn't have an impact on safety or architecture and a minor one on verbosity. Things like proper optionals, ADTs, data classes, pattern matching, etc. are more important. It would still be nice to have, at some point.

ivnsch avatar Mar 21 '19 08:03 ivnsch

According to our Q1 survey

@Hixie Which survey was that and how did you advertise it? I hope it's not a Flutter-only survey that excludes all other Dart developers, was it? I usually keep a close eye on things in the Dart community, but I didn't see anything.

mnordine avatar Mar 21 '19 12:03 mnordine

When I say "our" I mean "Flutter's" (I'm the Flutter TL), sorry for any ambiguity.

FWIW, in the same survey about 80% of the respondents said they would like breaking changes in general if it improved APIs. The numbers above are the numbers for the specific ideas we asked about. We hope to have a blog post out soon with more details on the results of the survey. The survey is advertised in Flutter channels such as the flutter mailing lists and our Twitter feed.

Hixie avatar Mar 21 '19 17:03 Hixie

That's good data to have, but is somewhat confounded:

  • (As far as I know) it only polls existing Flutter users, so has survivorship bias.
  • It frames it in terms of a breaking change, which implies a level of pain that might not be true for how the feature is actually shipped. For example, optional semicolons was technically a breaking change in Go when they shipped it, but I don't recall many users feeling that way about it. A sufficiently smooth migration path is indistinguishable from a non-breaking change, I think. At the very least, there are certainly shades of gray between "breaking" and "non-breaking".

But, in general, yes, optional semicolons aren't a clear huge win.

munificent avatar Mar 21 '19 17:03 munificent

When I say "our" I mean "Flutter's"

Yeah, that was my worry. I understand it's a priority for you, and I don't mean it personally of course, but applying Flutter-only polls to general language issues excludes a ton of users, who also use it in non-Flutter contexts (thus giving a more complete picture). Why not poll all Dart users instead of just a subset of them in the first place?

mnordine avatar Mar 21 '19 18:03 mnordine

I welcome other sources of data if we have any. I'm just reporting on the data I have. I personally am pretty neutral on the whole topic so long as it doesn't break patterns like the ones I've mentioned before, such as:

  return _convertToPixels(margin.left * (widget.leftScaleFactor ?? 1.0))
       + _convertToPixels(margin.right * (widget.rightScaleFactor ?? 1.0))
       + innerImage.padding.horizontal
       - size.width * 2.0
       - 8.0;

Hixie avatar Mar 21 '19 18:03 Hixie

Whenever there is a new proposal, it's a good thing to have optional semicolons, and the voting results are obvious.

But somebody always comes out to stop it.

Maybe some people think that aesthetic improvement is not a great victory, and there should be no high priority.

But judging from the voting results, most people still look forward to improvement.

There may be some border situations that need to be dealt with, and I will support them, but don't try to ignore them and prevent them from progressing.

yisar avatar Mar 24 '19 02:03 yisar

prevent them from progressing.

I don't think this is the right mental model to have. The language doesn't progress spontaneously on its own. It requires a lot of hard work designing proposals. The main reason optional semicolons aren't going forward right now is because no one has been able to come up with a clean, precise proposal that works well enough for them. In the absence of that, there is no way to progress, but it's not defined what we would progress to.

munificent avatar Mar 25 '19 17:03 munificent

Please make this real. #prayforoptionalsemicolon

hlung avatar Mar 31 '19 17:03 hlung

I'm very happy that semicolons are mandatory in Dart code. It makes the code much easier to read. The end of statements (possibly spread over multiple lines) is something I never need to figure out thanks to the explicit semicolons.

tedhenry100 avatar Apr 09 '19 04:04 tedhenry100