std.enum
Enum
Enforces that the value is an enum (either a tag or a variant).
Examples
('Foo | std.enum.Enum)
# => 'Foo
('Bar 5 | std.enum.Enum)
# => 'Bar 5
("tag" | std.enum.Enum)
# => error
from_tag_and_arg | { arg | optional, tag | String, } -> Enum
Creates an enum from a string tag and an optional argument. If the arg
field is omitted, a bare enum tag is created.
std.enum.to_tag_and_value
provides the inverse transformation,
extracting a string tag and an argument from an enum.
Examples
std.enum.from_tag_and_arg { tag = "Foo", arg = "arg" }
# => ('Foo "arg")
std.enum.from_tag_and_arg { tag = "http" }
# => 'http
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
map | (Dyn -> Dyn) -> Enum -> Enum
Maps a function over an enum variant's argument. If the enum doesn't have an argument, it is left unchanged.
Examples
std.enum.map ((+) 1) ('Foo 42)
# => 'Foo 43
std.enum.map (fun x => 1) 'Bar
# => 'Bar
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
to_tag_and_arg | Enum -> { arg | optional, tag | String, }
Converts an enum to record with a string tag and an optional argument. If
the enum is an enum tag, the arg
field is simply omitted.
std.enum.from_tag_and_arg
provides the inverse transformation,
reconstructing an enum from a string tag and an argument.
Examples
std.enum.to_tag_and_arg ('Foo "arg")
# => { tag = "Foo", arg = "arg" }
std.enum.to_tag_and_arg 'http
# => { tag = "http" }