Class LiteralTargetType

java.lang.Object
org.ek9lang.compiler.phase3.LiteralTargetType

final class LiteralTargetType extends Object
THE declared (target) type of a List or Dict literal - use it, do not re-derive the walk.

When the developer has been explicit - animals as List of Animal: [ Dog() ] - the literal IS that type, whatever its elements happen to be; each element is then checked against the declared element type exactly as a single assignment would be. Without a declaration the literal infers the common type of its elements, as before. Inferring from the elements alone made [ Dog(), Cat() ] a List of Animal but [ Dog() ] a List of Dog that the declaration then rejected - removing an element from a correct program broke it.

A target exists for: a declaration with a type (x as T: literal, which includes a returning declaration), a plain assignment to an identifier (x := literal, :, =, :=?), and a literal nested in a targeted literal (a List's element, a Dict's key or value) - so List of List of Animal: [ [ Dog() ] ] types the inner literal too. Assigning an existing List of Dog to a List of Animal stays invariant: that is aliasing, not a literal. Function arguments are deliberately not targets, because a literal's type takes part in choosing between overloads.

  • Constructor Details

  • Method Details

    • listElementType

      Optional<List<ISymbol>> listElementType(EK9Parser.ListContext ctx)
      The declared type parameters of the List literal at this context, if it has a List target.
    • dictKeyValueTypes

      Optional<List<ISymbol>> dictKeyValueTypes(EK9Parser.DictContext ctx)
      The declared key and value types of the Dict literal at this context, if it has a Dict target.
    • elementFitsOrError

      void elementFitsOrError(org.antlr.v4.runtime.ParserRuleContext elementCtx, ISymbol element, ISymbol declaredType, SemanticErrorReporter errorListener)
      An element of a targeted literal must be assignable to the declared element (or key/value) type - exactly the test a single assignment applies. The error is reported AT the element, naming the declared type.