Implicit member expressions #1062
Labels
feature
Proposed language feature that solves one or more problems
state-duplicate
This issue or pull request already exists
When referencing either a class's static member or an enum variant, the member has to be accessed using
TypeName.member
.If type inference determined the required type, in many cases it's obvious what the type should be. In those cases, being able to omit the type name when referencing a member would make for much shorter code, just
.member
in this case.This is similar to the "Implicit member expressions" feature in Swift.
Here are some usage examples:
This would be useful in lots of places in the real world – basically everywhere where enums or static special values are used.
In Flutter, for example, one could imagine being able to write
Edge cases
The following case is worth considering:
I'm not sure yet whether we'd need to handle this in a special way or how to handle this – maybe force a space before the expression as in
... .yummyFruits
?Semantically, there is no ambiguity either way, so just making sure dartfmt produces a useful result is probably the most viable option.
What are the benefits/dangers?
Exhaustive variants and default values both need to be expressed in a lot of scenarios. This feature would impact all of those cases.
Regarding readability concerns, it's easy to come up with an example such as this:
That is a valid concern – on the caller side, it's no longer clear that the parameter is a
Ripeness
. But the same problem has already been solved for non-enum parameters: Named parameters are used in cases where positional parameters don't clearly express the intent. Additionally, you can still use the old syntax ofshouldBuy(Ripeness.unripe)
, if you prefer.In the long term, I believe, linting rules should be put in place to encourage the use of the new, shorter syntax.
Social impact / transition
To experienced Dart developers, this saves some keystrokes, but more importantly, makes code more succinct and readable by reducing visual clutter.
Swift developers could continue using this feature.
New developers coming from other non-Swift-languages could continue writing code as usual but could get gently nudged towards using this feature by linting rules.
Novice Dart developers reading code are maybe startled at first by the new syntax. By definition, this syntax is only used where the type can be inferred from context though, so in most/all cases, the intent of the usage is still clear.
The text was updated successfully, but these errors were encountered: