Predicator.Errors.TypeMismatchError (predicator v9.0.1)

Copy Markdown View Source

Error struct for type mismatch errors in Predicator evaluation.

This error occurs when an operation receives values of incorrect types, such as trying to perform arithmetic on strings or logical operations on integers.

Fields

  • message - Human-readable error description
  • expected - The type that was expected (e.g., :integer, :boolean)
  • got - The actual type(s) received (single type or tuple for binary operations)
  • values - The actual value(s) that caused the error (optional, for debugging)
  • operation - The operation that failed (e.g., :add, :logical_and)
  • position - {line, column} of the source token that produced the failing instruction, when a position table was available (optional)
  • span - the source text the failing instruction's AST node covers, when the program was compiled with spans (optional). position names the token to blame; span is what to underline.

Examples

%Predicator.Errors.TypeMismatchError{
  message: "Arithmetic add requires integers, got "hello" (string) and 5 (integer)",
  expected: :integer,
  got: {:string, :integer},
  values: {"hello", 5},
  operation: :add
}

%Predicator.Errors.TypeMismatchError{
  message: "Unary minus requires an integer, got "text" (string)",
  expected: :integer,
  got: :string,
  values: "text",
  operation: :unary_minus
}

Summary

Functions

Creates a type mismatch error for binary operations.

Creates a type mismatch error for unary operations.

Creates a type mismatch error for a unary operation that accepts more than one type, spelling the accepted set out in the message while expected stays the single normative atom a consumer matches on.

Types

t()

@type t() :: %Predicator.Errors.TypeMismatchError{
  expected: atom(),
  got: atom() | {atom(), atom()},
  message: binary(),
  operation: atom(),
  position: Predicator.Types.position() | nil,
  span: Predicator.Types.span() | nil,
  values: term()
}

Functions

binary(operation, expected, arg1, arg2)

@spec binary(atom(), atom(), {atom(), atom()}, {any(), any()}) :: t()

Creates a type mismatch error for binary operations.

unary(operation, expected, got, value)

@spec unary(atom(), atom(), atom(), any()) :: t()

Creates a type mismatch error for unary operations.

unary(operation, expected, got, value, expected_text)

@spec unary(atom(), atom(), atom(), any(), String.t()) :: t()

Creates a type mismatch error for a unary operation that accepts more than one type, spelling the accepted set out in the message while expected stays the single normative atom a consumer matches on.

store accepts a string or an integer segment but reports expected: :string, the mirror of bracket_access's key rule (docs/isa.md section 5); a message built from the atom alone would tell a user something false about what the opcode accepts.

Examples

iex> error = Predicator.Errors.TypeMismatchError.unary(:store, :string, :boolean, true, "a string or an integer")
iex> {error.expected, error.message}
{:string, "Assignment requires a string or an integer, got true (boolean)"}