Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

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());
    }
like image 994
Kasandrop Avatar asked Jun 03 '20 14:06

Kasandrop


People also ask

Can a class name start with dollar sign?

Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

How do you use the dollar sign in darts?

Since you want the interpolation for "$dollars", you can't use "$" literally, so you need to escape it: int dollars = 42; print("I have \$$dollars."); If you don't want to use an escape, you can combine the string from raw and interpreted parts: int dollars = 42; print(r"I have $" "$dollars.");

How do you show the dollar in flutter?

\$ is the correct escape sequence for a dollar sign. If you don't need interpolation in your string you can also use a raw string literal by prefixing with r.

What is _$ in Dart?

By convention generated code starts with “_$”, to mark it as private and generated. So the generated implementation will be called “_$User”. To allow it to extend “User” there will be a private constructor for this purpose called “_”: === user.dart ===abstract class User {


1 Answers

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.

like image 89
lrn Avatar answered Nov 15 '22 10:11

lrn