What is the semantics inside?

I see links to _internal in examples, for example:

class Symbol {
    final String name;
    static Map<String, Symbol> _cache;

    factory Symbol(String name) {
        if (_cache == null) {
        _cache = {};
     }

     if (_cache.containsKey(name)) {
        return _cache[name];
     } else {
        final symbol = new Symbol._internal(name);
        _cache[name] = symbol;
        return symbol;
      }
    }

  Symbol._internal(this.name);
}

I realized from the code that this is a private accessible constructor. The last line Symbol._internal(this.name);seems a bit confusing because it looks like an operator inside the class body, not inside the method body, which makes me believe this definition of an internal constructor without the method body.

Are my assumptions correct?

+9
source share
1 answer

The _internal construct is simply the name often given to constructors that are private to the class (the name does not have to be ._internal, you can create a private constructor using any Class._someName construct ).

, :

class Person {

    final String name;
    static Map<String,Person> _cache;

    factory Person(String name) {
        if(_cache === null) {
            _cache = new Map<String,Person>();
         }
         if(_cache[name] === null]) {
            _cache[name] = new Person._internal(name); 
         }
         return cache[name];
    }

    Person._internal(this.name);
}

, Dart _ , . , :

_globalToThisLibaryOnly() {
    print("can only be called globally within this #library");
}

, .

+20

All Articles