Allow var type id
in non-declaring constructor parameter lists?
#4179
Labels
var type id
in non-declaring constructor parameter lists?
#4179
@munificent has a glorious write-up of declaring constructors: #4169
It allows
var int x
as a parameter in declaring constructors, as a way to introduce a mutable instance variable. 🎉The grammar allows
'var' <type> <identifier>
in every parameter, and the feature spec then disallows avar
+type from occurring in a non-declaring constructor using prose (it's a compile-time error if a parameter containingvar
occurs in a non-declaring constructor parameter list).I suggest we just allow it everywhere. It's easier than disallowing it, and the meaning is obvious. It makes
var
andfinal
(finally) be symmetric.It is unnecessary, because
var int x
means the same asint x
, butvar x
andx
also means the same thing in parameters, and we allow both.One argument against is that if anyone does write
var int x
as a normal parameter, someone else might initially read it as a field parameter, even though it's not a declaring constructor.Most likley, nobody will write that. They don't write
var x
today, even though it's valid. They don't writefinal int x
in parametes, even in code that insists on every local variable beingfinal
if it can be.I don't think we'll see an up-tick in occurrences of
final int x
in normal parameters, or occurrences ofvar int x
at all, so the confusion risk should be minimal.That also means that there is no benefit to allowing the syntax. (Hypothetically, allowing
var int x
everywhere means that you can safely change anyfinal
tovar
in a variable declaration, without having to do anything else. Might be easier for code generators. But I don't expect users to just blindly replacefinal
withvar
without looking at a declaration.)It does mean that the same syntax means different things in different places, which is usually bad.
That's already the case for
final int x
in declaring constructors, and this way, the issue would be symmetric invar
andfinal
. It'll be one less exception to remember, you can think ofvar
andfinal
as symmetric.(Rather than: "You can use
var int x
andfinal int x
in declaring constructors to declare avar
/final
field. In other parametersvar int x
is an error. In other constructorsfinal int x
means a final parameter.", it becomes "You can use(var|final) int x
in declaring constructors to declare avar
/final
field. In other parameters,(var|final) int x
just introduces a var/final parameter variable." It's symmetric, if you understand the difference betweenvar
andfinal
, you can apply the consistenly, so I'd count this as a complexity score of 2 vs 3 when disallowingvar int x
elsewhere. It all counts!)It's only parameters, though, you can't actually declare a field as
var int x;
. But you can writevar (int x) = ...
. I'd be fine with allowingvar int x;
for variables too.(Allowing
var int x;
may be a stepping stone tofinal
-by-default, if we ever want that. I do, don't know about "we".)Options:
var int x
as parameter everywhere. (My proposal.)late var int x = 42;
. (Maybe, eventually.)final
in non-declaring constructor parameters. (Suggested in feature spec.)WDYT?
@dart-lang/language-team
The text was updated successfully, but these errors were encountered: