我刚刚升级了我的 Dart 编辑器 (0.5.16_r23799),并且没有错误/警告的代码不再存在。
class Fubar {
const BAR = 1000000;
Fubar(){
}
}
以
const
开头的行有标记和这条消息:Only static fields can be declared as 'const'
我读了这个 ch02-final-const , 那里空无一物。
本帖dart-const-static-fields说
const modifier implies static
, 如果我们不能使用 const
没有 static
,我们应该使用 final
相反?...但是我错过了什么文档/帖子?打算这样做:Fubar f = new Fubar();
some = f.BAR;
最佳答案
这是最近在 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:
- No const instance fields.
- Metadata is statically scoped.
- 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.
关于dart - 为什么只能将静态字段声明为 'const' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17074897/