Variant structure

March 8, 2026 ยท View on GitHub

Up to index

The Variant structure provides operations for working with the variant type, which can hold values of any type in a dynamically-typed fashion.

Synopsis

datatype variant
  = UNIT
  | BOOL of bool
  | INT of int
  | REAL of real
  | CHAR of char
  | STRING of string
  | LIST of variant list
  | BAG of variant list
  | VECTOR of variant list
  | VARIANT_NONE
  | VARIANT_SOME of variant
  | RECORD of (string * variant) list
  | CONSTANT of string
  | CONSTRUCT of string * variant

val parse : string -> variant
val print : variant -> string

datatype variant

is a dynamically-typed value that can hold any Morel value.

parse

parse s parses a variant from its string representation.

The string is in the format produced by the print function, and therefore parse (print v) = v for all variant values v.

print

print v (or v.print ()) converts a variant to a string.

For example, print (BOOL true) returns "BOOL true"; print (LIST [INT 1, INT 2]) returns "LIST [INT 1, INT 2]".