为什么只能将静态字段声明为“ const”? [英] Why only static fields can be declared as 'const'?

查看:105
本文介绍了为什么只能将静态字段声明为“ const”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级了Dart编辑器(0.5.16_r23799),并且不再提供错误/警告的代码了。

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(){   
  }
}

const 有标记和此消息:


仅静态字段可以声明为' const'

Only static fields can be declared as 'const'

我阅读了 ch02-final-const ,那里什么也没有。

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

这篇帖子 dart-const-static-fields 表示 const修饰符意味着静态,如果我们不能在没有静态的情况下使用 const ,则应使用 final 代替吗?...但是我错过了什么文档/帖子?打算这样做:

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;


推荐答案

这是与 6月4日Dart语言设计会议的注释


const实例变量

Gilad认为除了作用域外,它们应该像静态一样工作。显然,这使实例元数据的VM实现变得复杂。三种解决方案:

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. 没有const实例字段。

  2. 元数据是静态范围的。

  3. 尝试正确执行操作。

喜欢1。我说1为用户简化了事情。现在,人们对静态最终const等感到困惑。Gilad同意1。

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.

我问语法是静态const还是 const?用户必须对常量执行静态操作时会感到困惑。

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

Lars说他们很困惑,因为他们不了解系统。要求静态可以帮助他们了解发生了什么。

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

这篇关于为什么只能将静态字段声明为“ const”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆