"var" as explicit non-final marker

9 views
Skip to first unread message

Michael Hendricks

unread,
Oct 26, 2011, 9:41:25 PM10/26/11
to General Dart Discussion
Proposal:

Allow "var" before any type declaration as an explicit statement that the developer intends the variable to be non-final.  The presence of "var" before a type has no effect on the program's behavior because it only declares what is already the default.  The "var" is only there to document developer intent and for future static analysis tools.

If I've read the spec correctly, I intend section 5 to have the following, modified definition:

  finalVarOrType:
    final type? |
    var type? |
    type
  ;


Motivation:

Some developers prefer to work with single-assignment variables, but manually marking each variable as final is a burden.  Making all variables final by default burdens other developers (see earlier discussions[1][2]).  Without changes to Dart, "final-ists" could write a static analysis tool to warn about multiple-assignment:

  int x = 7;
  int y = 1;
  x = x + y;  // tool warns: multiple assignment to variable "x"

Without this proposal, the tool could use special comments to exempt specific variables from the restriction.

  int x = 7; /* {non-final} */

With this proposal, the developer accomplishes the same thing with

  var int x = 7;


Since all existing Dart code is still valid under this proposal, I can't see any additional burden for developers coming from Java/JS/C#.  Developers wanting to enforce single-assignment carry the burden of their own preference since they have to write the static analysis tool (or compiler option).

Any thoughts or questions?  Is such a change acceptable?



-- 
Michael

Michal Mocny

unread,
Oct 27, 2011, 1:23:27 PM10/27/11
to General Dart Discussion
I very much expected this to be the case already. I support this
proposal.

> [1]:https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thr...
> [2]:https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thr...
>
> --
> Michael

Michael Hendricks

unread,
Oct 28, 2011, 2:45:18 PM10/28/11
to Michal Mocny, General Dart Discussion
Reply all
Reply to author
Forward
0 new messages