简体   繁体   中英

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? I found similar question on stackoverflow but it was asked in 2012 and the answer is not valid any more.

Example 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 $ . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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