Enhanced C#
Language of your choice: library documentation
Properties | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
Loyc.Symbol Class Reference

Represents a symbol, which is a singleton string that supports fast comparisons and extensible enums. More...


Source file:
Inheritance diagram for Loyc.Symbol:
Loyc.IReferenceEquatable Loyc.Syntax.ParsingMode

Remarks

Represents a symbol, which is a singleton string that supports fast comparisons and extensible enums.

Call GSymbol.Get() to create a Symbol from a string, or GSymbol.GetIfExists() to find a Symbol that has already been created.

Symbols can be used like a global, extensible enumeration. Comparing symbols is as fast as comparing two integers; this is because '==' is not overloaded–equality is defined as reference equality, as there is only one instance of a given Symbol.

Symbols can also be produced in namespaces called "pools". Two Symbols with the same name, but in different pools, are considered to be different symbols. Using a derived class D of Symbol and a SymbolPool<D>, you can make Symbols that are as type-safe as enums.

A Symbol's ToString() function formerly returned the symbol name prefixed with a colon (:), following the convention of the Ruby language, from which I got the idea of Symbols in the first place. However, since Symbols are commonly used for extensible enums, I decided it was better that ToString() return just the Name alone, which makes Symbol more suitable as a drop-in replacement for enums.

Symbols are also useful in compilers and Loyc trees, where there may be a performance advantage in comparing identifiers by reference rather than character-by-character.

Properties

string Name [get]
 Gets the name of the Symbol. More...
 
SymbolPool Pool [get]
 Gets the SymbolPool in which this Symbol was created. More...
 
bool IsGlobal [get]
 Returns true if this symbol is in the global pool (GSymbol.Pool). More...
 
int Id [get]
 Returns a numeric ID for the Symbol. More...
 

Public Member Functions

override string ToString ()
 
override int GetHashCode ()
 
override bool Equals (object b)
 
bool Equals (Symbol b)
 
int CompareTo (Symbol other)
 

Static Public Member Functions

static operator Symbol (string s)
 
static operator string (Symbol s)
 
static operator Symbol (UString s)
 
static operator UString (Symbol s)
 
static Symbol For (string s)
 Alias for GSymbol.Get(string). This function was introduced to match the equivalent ES6 API Symbol.for("string") More...
 

Protected Member Functions

 Symbol (Symbol prototype)
 For use by a derived class to produce a statically-typed enumeration in a private pool. See the example under SymbolPool (of SymbolEnum) More...
 

Constructor & Destructor Documentation

Loyc.Symbol.Symbol ( Symbol  prototype)
inlineprotected

For use by a derived class to produce a statically-typed enumeration in a private pool. See the example under SymbolPool (of SymbolEnum)

Parameters
prototypeA strictly temporary Symbol that is used to initialize this object. The derived class should discard the prototype after calling this constructor.

References Loyc.Symbol.Name.

Member Function Documentation

static Symbol Loyc.Symbol.For ( string  s)
inlinestatic

Alias for GSymbol.Get(string). This function was introduced to match the equivalent ES6 API Symbol.for("string")

Referenced by Loyc.LLParserGenerator.LLParserGenerator.GenerateCodeVisitor.Visit().

Property Documentation

int Loyc.Symbol.Id
get

Returns a numeric ID for the Symbol.

Normally the Id is auto-assigned and the Id corresponding to a particular Name may vary between different runs of the same program. However, SymbolPool.Get(string, int) can be used to assign a specific Id to a Name when setting up a new pool.

Referenced by Loyc.SymbolPool< SymbolE >.Get().

bool Loyc.Symbol.IsGlobal
get

Returns true if this symbol is in the global pool (GSymbol.Pool).

string Loyc.Symbol.Name
get
SymbolPool Loyc.Symbol.Pool
get

Gets the SymbolPool in which this Symbol was created.