std.enum

is_enum_tag : Dyn -> Bool

Checks if a value is an enum tag. Enum variants (applied to an argument) aren't considered enum tags.

Examples

std.enum.is_enum_tag 'foo
  => true
std.enum.is_enum_tag 'FooBar
  => true
std.enum.is_enum_tag "tag"
  => false
std.enum.is_enum_tag ('Foo "arg")
  => false

is_enum_variant : Dyn -> Bool

Checks if a value is an enum variant. Bare enum tags (not applied to an argument) aren't considered enum variants.

Examples

std.enum.is_enum_variant ('Foo "arg")
  => true
std.enum.is_enum_variant ('Http {version = "1.1"})
  => true
std.enum.is_enum_variant 'foo
  => false
std.enum.is_enum_variant [1, 2, 3]
  => false

Tag

Enforces that the value is an enum tag.

Examples

('foo | std.enum.Tag) =>
  `foo
('FooBar | std.enum.Tag) =>
  `FooBar
("tag" | std.enum.Tag) =>
  error

TagOrString

Accepts both enum tags and strings. Strings are automatically converted to an enum tag.

TagOrString is typically used in conjunction with an enum type, to accept both actual enum tags and tags represented as strings (e.g. coming from a JSON serialization).

Warning: contracts are applied in-order. The pattern described here requires that TagOrString is applied before the corresponding enum contract. Thus, TagOrString must appear before the enum contract in the annotations, as in the example below.

Examples

let Schema = {
  protocol
    | std.enum.TagOrString
    | [| 'http, 'ftp |],
  port
    | Number,
  method
    | std.enum.TagOrString
    | [| 'GET, 'POST |]
} in
let serialized =
  m%"
    {"protocol": "http", "port": 80, "method": "GET"}
  "%
  |> std.deserialize 'Json
in

serialized | Schema