Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the semantics of _internal?

Tags:

dart

I keep seeing references to _internal in examples like the following:

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've gathered from the code that it's a privately accessible constructor. The last line Symbol._internal(this.name); seems a bit confusing because it appears to be a statement within the class body and not within a method body, leading me to believe it's actually the internal constructor definition without a method body.

Are my assumptions correct?

like image 481
w.brian Avatar asked Jun 20 '12 03:06

w.brian


People also ask

What is _internal () in Dart?

_Internal const internal. Used to annotate a declaration which should only be used from within the package in which it is declared, and which should not be exposed from said package's public API.

What is_ internal?

The _internal construction is just a name often given to constructors that are private to the class (the name is not required to be . _internal you can create a private constructor using any Class.

What is factory in Dart?

In Dart, we use the factory keyword to identify a default or named constructor. We use the factory keyword to implement constructors that do not produce new instances of an existing class.

How do you make a constructor private in Dart?

A constructor can be made private by using (_) underscore operator which means private in dart. The same theory applied while extending class also, It's also impossible to call the private constructor if it declares in a separate file.


1 Answers

The _internal construction is just a name often given to constructors that are private to the class (the name is not required to be ._internal you can create a private constructor using any Class._someName construction).

For example the following code only allows you to create new persons from outside the class using a caching constructor:

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); } 

In general Dart treats any _construction as private to either the class or the library that contains it. For example you can define as a global function like this:

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

which can be called from any file that is sourced within the library that defines it.

like image 166
Lars Tackmann Avatar answered Oct 12 '22 17:10

Lars Tackmann