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

◆ Symbol()

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.

Member Function Documentation

◆ For()

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")

Property Documentation

◆ Id

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.

◆ IsGlobal

bool Loyc.Symbol.IsGlobal
get

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

◆ Name

string Loyc.Symbol.Name
get

◆ Pool

SymbolPool Loyc.Symbol.Pool
get

Gets the SymbolPool in which this Symbol was created.