dart-lang / language

Design of the Dart language
Other
2.65k stars 202 forks source link

Optional Semicolons #69

Open mit-mit opened 5 years ago

mit-mit commented 5 years ago

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.

yjbanov commented 5 years ago

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.

mit-mit commented 5 years ago

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

mit-mit commented 5 years ago

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

feinstein commented 5 years ago

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;
munificent commented 5 years ago

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.)

egdelgadillo commented 5 years ago

I agree, this should be a feature

korgara commented 5 years ago

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.

egdelgadillo commented 5 years ago

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

munificent commented 5 years ago

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.

Buerkut commented 5 years ago

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.

jamesporter commented 5 years ago

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).

nayan2823 commented 5 years ago

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.

munificent commented 5 years ago

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

rodolfoggp commented 5 years ago

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...

ivnsch commented 5 years ago

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 commented 5 years ago

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?

leafpetersen commented 5 years ago

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.

yisar commented 5 years ago

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.

munificent commented 5 years ago

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.

Hixie commented 5 years ago

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.

ivnsch commented 5 years ago

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.

mnordine commented 5 years ago

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.

Hixie commented 5 years ago

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.

munificent commented 5 years ago

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

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

mnordine commented 5 years ago

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?

Hixie commented 5 years ago

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;
yisar commented 5 years ago

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.

munificent commented 5 years ago

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.

hlung commented 5 years ago

Please make this real. #prayforoptionalsemicolon

tedhenry100 commented 5 years ago

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.

hlung commented 5 years ago

@tedhenry100 I have been working with Swift (which has optional semicolon) for more than 3 years now and for me I have no readability issue. A newline is enough to signify end of a statement, no need for another semicolon. It's just extra noise.

yisar commented 5 years ago

First of all, I strongly oppose semicolons and support non-semicolons, but I can also see a small number of semicolon enthusiasts.

Therefore, the optional semicolon is more acceptable.

The difficulty now is that there is no good plan to achieve it.

But I think in most cases, semicolons can be chosen, and semicolons can still be retained at certain special boundaries.

Like typescript, most of them can be omitted. For loops and so on, semicolons are still retained.

tedhenry100 commented 5 years ago

@hlung I've been working in Swift for the past 2.5 years. Now learning Dart, I'm glad that semicolons are mandatory in Dart. As they say, there's no accounting for taste.

kevmoo commented 5 years ago

@munificent @leafpetersen @lrhn @eernstg – should be moved to language repo?

munificent commented 5 years ago

We can if you'd like.

hlung commented 5 years ago

@tedhenry100 well, if semicolon is matter of taste then it should not be mandatory. And it should be optional.

naiveai commented 5 years ago

I almost like this proposal, but I fear we'll end up with the dumb semicolon wars that are caused by JavaScript's ASI. Also, the arguments used by most people proposing this are mostly just "all modern languages don't have semicolons", which doesn't make much sense to me. Semicolon insertion takes a microsecond out of your day and it can easily be automated.

munificent commented 5 years ago

caused by JavaScript's ASI.

That's because JavaScript's ASI rules are bananas. Other languages with optional semicolons — Python, Ruby, Scala, Go, etc. — do not have dumb insertion rules and do not have wars over the use of semicolons.

ditiem commented 5 years ago

I am really surprised nowadays how many people like the idea of not typing semicolons.

Semicolon, to me, is a very important declaration from the programmer saying: 'I want this statement to end here'. After reading lots of code with semicolon, I found my brain can read faster by knowing how many line(s) have to be processed before passing to read the next statement.

It is not about ambiguity neither aesthetic. A line that does not end in a semicolon is like "unfinished job" to me. I believe (and unfortunately this cannot be demonstrated) the code ends up better structured when someone is forced to say when the statement finish. Otherwise the programmer tends to think too fast and make more mistakes.

munificent commented 5 years ago

I found my brain can read faster by knowing how many line(s)

The fact that you say "line" here, though, implies that it is actually the newline that is the signal your brain is relying on, not the semicolon.

mockturtl commented 5 years ago

the idea of not typing semicolons

At the risk of being pedantic, it isn't the typing as such. Readability suffers a little. What's genuinely unpleasant is hunting down mysterious, nearly-invisible errors, moving the cursor to end-of-line while fighting smart parentheses, and undoing saves because dartfmt newline-bombed me. :smile:

It feels like it would help for auto-completion tools to append semicolons more eagerly.

Claus1 commented 5 years ago

It seems semicolons are very valuable for Dart team (7 years resisting developers desire). But can they make at least optional semicolon before } ? It is very annoying getting errors (flutter) from the compiler that I have to insert ';' somewhere in )})} .

Example such code

onPressed: () {

                Navigator.push(context, MaterialPageRoute(builder: (context){
                  return Screen2();
                })
         );}), //it wants ; here, otherwise an error!

Very very annoying!

munificent commented 5 years ago

It seems semicolons are very valuable for Dart team (7 years resisting developers desire).

This isn't the right metaphor to describe the behavior. When you say "resist", that implies the Dart team has to spend effort to not make semicolons optional, but the opposite is the case. Semicolons are already mandatory in Dart, so to make them optional requires a significant amount of effort to change. The inertia is towards mandatory semicolons.

We (I in particular) have put quite a bit of work into trying to come up with a design for optional semicolons in Dart that plays nice with the rest of the language grammar, but unfortunately we haven't been able to come up with anything that harmonizes with the rest of the language. Other languages with optional semicolons tend to have simpler grammars with mandatory keywords for things like variable and member declarations. Dart's C/Java-derived grammar is a much larger challenge.

rrousselGit commented 5 years ago

This has been suggested before but it didn't receive answers:

What about moving that in the scope of dartfmt instead? We could keep the ; required, but have a tool that inserts them when obvious.

As a tool, it doesn't have to be perfect. It's fine if the tool is unable to add the ; in some situations.

EugenePisotsky commented 5 years ago

It's fine if the tool is unable to add the ; in some situations

sounds like it will be very confusing

mit-mit commented 5 years ago

@rrousselGit any tool -- whether it's a formatter or a compiler -- is going to have to be able to correctly parse it's input and determine where the implicit semicolons are. So I'm not sure the dartfmt approach is viable.

@Claus1 there are an endless number of things any language team can choose to work on; the tricky part is selecting the right ones. In this case we spent a significant amount of time investigating what it would take to add optional semicolons to an existing language that wan't designed for this initially (the design of Dart goes back more than a decade). It turned out it would realistically involve a larger number of breaking changes to make change core parts of our syntax. For more details, see the detailed write-up. Having uncovered that we decided that now wasn't the right time to do that; we are working hard to get NNDB completed. Also, please refrain from making personal comments (see https://dart.dev/code-of-conduct for details; personal statements such as "Dart team do not work hard enough" are not appropriate).

rrousselGit commented 5 years ago

@mit-mit dartfmt or the autocomplete. The difference with a compiler is that the compiler has to be perfect and maintained over time. Whereas dartfmt/autocomplete could stick to the simple scenarios, and potentially be reverted if too difficult to maintain.

And since it requires no language modification, it could start as an experimental fork of _dartstyle without committing to anything.

mit-mit commented 5 years ago

@rrousselGit my main concern wasn't cost of implementation. It was ensuring the feature has a reasonable and predictable user experience. I don't think it's feasible, but maybe @munificent has thoughts on it?

ds84182 commented 5 years ago

I have a stopgap solution for this. I don't really agree with optional semicolons, but I think dartfmt could help out in this area.

I have an editor shortcut that invokes dartfmt, and many people have "Format on save" enabled in their editors. The issue I sometimes hit, however, is that I miss a semicolon, so the format fails.

Instead of the language gaining optional semicolons (or, losing semicolons... we aren't really gaining much here!), could dartfmt instead gain automatic semicolon placement? I think either side can agree that this would be a good compromise, and would allow us to further evaluate our decisions. But for implementation work, that is ultimately up to @munificent.

feinstein commented 5 years ago

That's literally what @rrousselGit suggested 6 posts before yours.