dart - 为什么只能将静态字段声明为 'const' ?

标签 dart dart-editor

我刚刚升级了我的 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-fieldsconst 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:

  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.

关于dart - 为什么只能将静态字段声明为 'const' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17074897/

相关文章:

dart - 当我从 Dart 中标记为异步的函数返回 Future 时会发生什么?

data-binding - 如何告诉聚合物 Dart 一个属性依赖于另一个属性

flutter - 为什么在初始化状态后不能刷新构建

dart - 如何配置Dart编辑器以生成 “.UXL”文件

dart - 当我的静态文件位于具有不同 URL 的 CDN 上时,如何使用 AngularDart?

flutter - flutter 中的compute()无效

dart - 无法在 Dart 聚合物 webapp 中重复工作

dart - Dart 1.8 中的异步/等待功能

dart - 使用 Dart 编辑器中的标志启动 Dartium

apache - 配置Apache以避免使用Dart进行跨域调用