【问题标题】:What does dollar sign $ mean at the beginning of the class name in Dart?Dart 中类名开头的美元符号 $ 是什么意思?
【发布时间】:2020-09-22 06:51:50
【问题描述】:

在生成的类中可以经常观察到美元符号。 类型也以$ 符号开头。我知道 Dart 语言允许类名包含美元符号,但也许它有一个我不知道的隐藏含义?我在 stackoverflow 上发现了类似的问题,但在 2012 年被问到,答案不再有效。

示例 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());
    }

【问题讨论】:

标签: flutter dart


【解决方案1】:

Dart 标识符可以包含$。它只是该语言的另一个字母,但传统上它是为生成的代码保留的。这允许代码生成器(在很大程度上)避免担心命名冲突,只要他们在他们创建的名称中的某处放置 $。

在this particular case 中,该名称仅用于表示原始程序中未出现的合成名称。它是由您找到它的地方描述的脱糖“生成”的。

【讨论】:

    猜你喜欢
    • 2020-01-20
    • 1970-01-01
    • 2012-01-10
    • 2017-07-22
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    相关资源
    最近更新 更多