Home > Net >  Why are Dart's type annotations on the left?
Why are Dart's type annotations on the left?

Time:06-25

New languages, TypeScript, Kotlin, Scala, and Rust all have type annotations on the right, but Dart has type annotations on the left. Is this designed that way for a reason or benefit? Is there a possibility that the type annotation will be changed to the right in the future roadmap or when Dart3 is developed? I like most of Dart, but I don't like this syntax. I have looked for a thread discussing this but could not find one. Is it discussed anywhere?

CodePudding user response:

The Dart syntax was inspired by Java/C#/JavaScript, which are all from the C-family of language syntax. Dart is in that family too.

Using a C-like syntax means putting types before identifiers in declarations, and using the placement to recognize what is a type and what is not. That's why you can use the same names for types and variables. (Not that you should, but the grammar doesn't care).

The goal was familiarity to users of those languages.

Dart also has other C-syntax idiosyncrasies, like the conditional operator ?/:, or the for(..;..;..).. loop, and ending statements with a ;. It's familiar, if that's the thing you are already familiar with.

Having a C-like syntax has its down-sides too, when it comes to adding new language features. Because the C syntax is so grammatically dense, there's not much room to add new syntax in the gaps. Most things already mean something, and are there for a reason (it's incredibly hard to, e.g., remove semicolons, because the grammar isn't built to be able to guess where a new statement start.) On the other hand, it's syntactically dense too, aka "not verbose". That's usually considered a good thing.

As for odds of a large syntax change, like moving types to the right, in Dart 3.0 ... I'd say 0.0%.

It has to be an incredibly valuable change to outweigh having to migrate every Dart file in existence. Simply moving types to a another place in the code isn't going to be that by itself. It would have to be a complete re-imagining of the entire language syntax. And should still keep supporting the old syntax forever. (Maybe Dart 10.0 NSE - New Syntax Experience!)

CodePudding user response:

Why are Dart's type annotations on the left?
Dart (Dash) was intended to replace JavaScript.
The developers of Dart did not hesitate (unlike the developers of other modern languages) to copy most of the syntax from JavaSctipt and Java.
The main reason is to copy the syntax of popular programming languages.
Not the best, but the easiest way.
Suffice it to recall the long-standing problem with the definition of the Function type.

  • Related