简体   繁体   English

Dart 中的 class 名称开头的美元符号 $ 是什么意思?

[英]What does dollar sign $ mean at the beginning of the class name in Dart?

Dollar sign can be observed in generated classes very often.在生成的类中可以经常观察到美元符号。 Also types start there with $ sign.类型也以$符号开头。 I know Dart language allows class names to contain dollar sign but maybe there is a hidden meaning of it which I am not aware of?我知道 Dart 语言允许 class 名称包含美元符号,但也许它有一个我不知道的隐藏含义? I found similar question on stackoverflow but it was asked in 2012 and the answer is not valid any more.我在 stackoverflow 上发现了类似的问题,但它在 2012 年被问到,答案不再有效。

Example 1.示例 1。

class Counter {
      int _counter = 0;
      int next() => ++_counter;
    }
    class Operation {
      void operate(int step) { doSomething(); }
    }


    class $OperationWithCounter = Operation with Counter;

    class Foo extends $OperationWithCounter{
      void operate([int step]) {
    super.operate(step ?? super.next());
    }

Dart identifiers can contain $ . Dart 标识符可以包含$ It's just another letter to the language, but it is traditionally reserved for generated code.它只是该语言的另一个字母,但传统上它是为生成的代码保留的。 That allows code generators to (largely) avoid having to worry about naming conflicts as long as they put a $ somewhere in the names they create.这允许代码生成器(在很大程度上)避免担心命名冲突,只要他们在他们创建的名称中放置一个$

In this particular case , the name is simply intended to represent a synthetic name that did not occur in the original program.这种特殊情况下,名称只是用来表示原始程序中没有出现的合成名称。 It was "generated" by the desugaring described where you found it.它是由您找到它的地方描述的脱糖“生成”的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM