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

I just updated the Dart Editor (0.5.16_r23799), and the code that was the error / warning was no longer there.

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

Lines starting with const have a marker and this message:

Only static fields can be declared as 'const'

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

This post dart-const-static-fields says that const modifier implies static , if we cannot use const without static , we should use final instead? ... but what document / message did I skip? Intend to do this:

 Fubar f = new Fubar(); some = f.BAR; 
+4
source share
1 answer

This is a recent change related to Notes from June 4th Dart Language Design Meeting :

const instance variables

The look of Gilad is that they should work as static, except for the field of view. Obviously, however, this complicates the implementation of the metadata of the virtual machine instance. Three solutions:

  • No const instance fields.
  • Metadata has a static scope.
  • Try to do it right.

Lars likes 1. I say that 1 makes it easier for users. Right now, people are 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 they have to do “static” with constants.

Lars says they are confused because they do not understand the system. The requirement of "static" will help them understand what is happening.

+4
source

Source: https://habr.com/ru/post/1485878/


All Articles