Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why only static fields can be declared as 'const'?

I just upgraded my Dart Editor (0.5.16_r23799), and code that was bug/warning free, is not anymore.

class Fubar {
  const BAR = 1000000;  
  Fubar(){   
  }
}

Lines beginning by const have marker and this message :

Only static fields can be declared as 'const'

I read this ch02-final-const, nothing there.

This post dart-const-static-fields says that const modifier implies static, if we can't use const without static, we should use final instead ?... but what doc/post I missed ? Intend to do this :

Fubar f = new Fubar();
some = f.BAR;
like image 742
Eric Lavoie Avatar asked Jun 12 '13 20:06

Eric Lavoie


1 Answers

This is a recent change related in the Notes From the June 4 Dart Language Design Meeting :

const instance variables

Gilad's view is that they should work like statics except for scoping. Apparently, though, it's complicating the VM implementation of instance metadata. Three solutions:

  1. No const instance fields.
  2. Metadata is statically scoped.
  3. Try to do it correctly.

Lars likes 1. I say 1 simplifies things for users. Right now, people get confused with static final const etc. Gilad is OK with 1.

I asked if the syntax would be "static const" or just "const"? Users get confused when having to do "static" with constants.

Lars says they are confused because they don't understand the system. Requiring "static" will help them understand what's going on.

like image 154
Alexandre Ardhuin Avatar answered Sep 22 '22 02:09

Alexandre Ardhuin