Class ConstantSymbol

java.lang.Object
org.ek9lang.compiler.symbols.Symbol
org.ek9lang.compiler.symbols.ConstantSymbol
All Implemented Interfaces:
Serializable, ISymbol, ISymbolNature, ITokenReference

public class ConstantSymbol extends Symbol
A very simple type that holds a single distinct value. For example PI = 3.242 would be a constant.
See Also:
  • Constructor Details

    • ConstantSymbol

      public ConstantSymbol(String name, boolean fromLiteral)
    • ConstantSymbol

      public ConstantSymbol(String name)
    • ConstantSymbol

      public ConstantSymbol(String name, ISymbol type, boolean fromLiteral)
    • ConstantSymbol

      public ConstantSymbol(String name, ISymbol type)
    • ConstantSymbol

      public ConstantSymbol(String name, Optional<ISymbol> type)
    • ConstantSymbol

      public ConstantSymbol(String name, Optional<ISymbol> type, boolean fromLiteral)
      Construction of a Constant.
  • Method Details

    • isFromLiteral

      public boolean isFromLiteral()
    • isConstant

      public boolean isConstant()
    • clone

      public ConstantSymbol clone(IScope withParentAsAppropriate)
      Description copied from interface: ISymbol
      Clone the symbol and re-parent if this symbol like a method should have a parent. Other symbols like VariableSymbols are un-parented
      Specified by:
      clone in interface ISymbol
      Overrides:
      clone in class Symbol
    • cloneIntoConstant

      protected ConstantSymbol cloneIntoConstant(ConstantSymbol newCopy)
    • equals

      public boolean equals(Object o)
      Description copied from class: Symbol
      Two symbols are the same symbol when they agree on name, category, genus, mutability AND the declaration site they came from.

      The source token is part of identity deliberately. Name alone is not enough: symbol names are unqualified here, so two unrelated variables both called count, or two types called Foo in different modules, would otherwise compare equal. The compiler relies on telling those apart - CodeFlowMap keys initialisation state on Map<IScope, Map<ISymbol, SymbolAccess>>, and conflating two same-named declarations there makes flow analysis attribute one variable's initialisation to another.

      That distinction used to be present ONLY in Symbol.hashCode(), which is why this looked like it worked: two same-named symbols compared equal yet hashed apart, so hash collections usually - but not reliably - kept them separate, while any direct equals call saw them as one. Encoding it here makes the two agree. See SymbolEqualsHashCodeContractTest.

      Overrides:
      equals in class Symbol
    • hashCode

      public int hashCode()
      Description copied from class: Symbol
      Mixes in exactly the fields Symbol.equals(Object) compares, and nothing else.

      A hashCode may legitimately use FEWER fields than equals (that only causes collisions, which are correct), but never MORE: any extra field makes two symbols that compare equal land in different buckets, so a HashSet silently holds visible duplicates and a HashMap lookup misses an entry that is present. This method used to mix in purity, which no equals at THIS level consults - purity belongs in FunctionSymbol, the level whose equals compares it. The source token stays, and is now matched by Symbol.equals(Object). See SymbolEqualsHashCodeContractTest.

      Overrides:
      hashCode in class Symbol
    • toString

      public String toString()
      Overrides:
      toString in class Symbol