declareMaybe function

MaybeInfo declareMaybe(
  1. Sort sort, {
  2. String? name,
})

Declares a datatype with a nothing and just constructor for the given sort, similar to the Haskell Maybe type.

Implementation

MaybeInfo declareMaybe(Sort sort, {String? name}) {
  final info = declareDatatype(
    name ?? 'Maybe ${getSortName(sort)}',
    [
      constructor('nothing', {}),
      constructor('just', {'value': sort}),
    ],
  );
  return MaybeInfo(
    sort: info.sort,
    nothing: ConstVar.func(info.constructors[0].constructor),
    isNothing: info.constructors[0].recognizer,
    just: info.constructors[1].constructor,
    isJust: info.constructors[1].recognizer,
    value: info.constructors[1].accessors[0],
  );
}