问题描述
我想创建一个与私有MethodMirror的简单名称相同的符号。但是,Symbol的文档指出,新Symbol的参数必须是有效的公共标识符。如果我尝试创建一个 const Symbol('_ privateIdentifier')
dart编辑器,则通知我该常量表达式的求值将引发异常-尽管程序运行正常,我
I want to create a symbol equal to that of a private MethodMirror's simplename. However, Symbol's documentation states the argument of new Symbol must be a valid public identifier. If I try and create a const Symbol('_privateIdentifier')
dart editor informs me that evaluation of this constant expression will throw an exception - though the program runs fine, and I am able to use it without any issues.
void main(){
//error flagged in dart editor, though runs fine.
const s = const Symbol('_s');
print(s); //Symbol("_s");
}
似乎镜像系统使用符号。
It seems the mirror system uses symbols.
import 'dart:mirrors';
class ClassA{
_privateMethod(){}
}
void main(){
var classMirror = reflect(new ClassA()).type;
classMirror.declarations.keys.forEach(print);
//Symbol("_privateMethod"), Symbol("ClassA")
}
由于过时的飞镖分析器,飞镖编辑器中的文档/错误标记是否是遗留错误?还是将来有计划执行此公共要求?还有另一种方法来创建唯一的标识符号,该标识符号将被最小化为与声明的简单名称相同的符号
Is the documentation/error flagging in dart editor a legacy bug due to an outdated dart analyzer? Or are there plans to enforce this public requirement in future? Is there another way to create a unique identifying symbol that will be minified to the same symbol as the declaration's simple name
推荐答案
不会抛出异常,则VM会在const Symbol构造函数中出现错误。
If it doesn't throw then the VM has a bug in the const Symbol constructor.
问题在于, _ s无法标识私有变量,而又不说出它是哪个库属于。由于这个原因,符号构造函数有第二个参数采用 LibraryMirror
,并且传递私有名称而不传递镜像也应该抛出异常。
如果不回避const构造函数的要求(不执行代码!),这在const构造函数中很难做到,这很可能是VM无法处理的原因。
The problem is that "_s" does not identify a private variable without also saying which library it belongs to. The symbol constructor has a second argument taking a LibraryMirror
for that reason, and passing in a private name without also passing in a mirror should throw.That's hard to do in a const constructor without side-stepping the requirements of a const constructor (no executing code!), which is likely why the VM doesn't handle it. It needs to be special-cased at the compiler level.
您还会发现 const Symbol('_ s')
与 #_ s
不同。后者为当前库创建一个私有符号,前者(如果运行)创建一个名为 _s的非私有符号,这实际上没有用。例如 print(identical(#_ s,const Symbol('_ s')));
打印false。
You will also find that const Symbol('_s')
is not the same as #_s
. The latter creates a private symbol for the current library, the former (if it runs) creates a non-private symbol with the name '_s', which is not really useful. For example print(identical(#_s, const Symbol('_s')));
prints false.
这篇关于具有专用标识符参数的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!