gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:22:20 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com