飞镖符号文字 [英] Dart symbol literals

查看:28
本文介绍了飞镖符号文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Zones上的文档,它具有以下代码:

I was reading this document on Zones and it has the following code:

runZoned(() {
  print(Zone.current[#key]);
}, zoneValues: { #key: 499 });

我试图找到更多的文档符号文字,但我找不到,它们非常稀缺.

I tried to find more docs symbol literals, but I couldn't, they are very scarce.

我输入了以下代码进行测试:

I typed this code for a test:

var a = #b;

我可以看到 a Symbol .

Symbol 文字如何工作?

它指的是什么?

仅通过创建一个,我就创建了一个新的唯一 Symbol ?

Just by creating one I create a new unique Symbol?

推荐答案

符号不是唯一的.它们旨在表示运行时的源名称,因此,符号文字 #a 的符号值(也可以写为 )代表标识符 a .

Symbols are not unique. They are intended to represent source names at runtime, so the symbol value of the symbol literal #a (which can also be written const Symbol("a")) represents the identifier a.

符号仅出现在Dart的动态部分中.特别是,它们用于命名参数的表示形式.html"rel =" nofollow noreferrer> Invocation 类(传递给 noSuchMethod 的参数)和 dart:mirrors ,它们也在那里使用,这就是它们最初引入的用途.

Symbols occur only in the more dynamic parts of Dart. In particular they are used in the representation of named arguments used by the Invocation class (the argument passed to noSuchMethod) and the Function.apply function. If your platform has access to dart:mirrors, they are also used there, and that was the original use they were introduced for.

在这些情况下,您不应该使用,但是在某些情况下,例如上面的 #key ,这很容易 提供带有自己名称的可复制(非唯一)令牌的方法.或者,如果您想要一个自己的库所独有的令牌,则可以为 private 名称 #_ foo 创建符号.这表示库私有的 _foo 标识符,其他任何库都无法复制.

You shouldn't need to use symbols outside of those cases, but in some situations, like the #key above, it's just an easy way to provide a reproducible (not unique) token that carries its own name. Or, if you want a token that is unique to your own library, you can make a symbol for a private name, #_foo. This represents the library-private _foo identifier, which no other library can reproduce.

具有不是字符串的源名称表示形式,例如,Dart to JavaScript编译器可以最小化名称,而不必在部署的代码中保留更长的源标识符.

Having a representation of source names that is not a string allows, e.g., a Dart to JavaScript compiler to minify the names and not have to retain the longer source identifiers in the deployed code.

这篇关于飞镖符号文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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