Branded types with checked constructors (Effect's Brand).
A brand is a typing.NewType: nominal under pyright, the plain value at
run time. Brand[Base, NewType] makes constructors for it: make and
check ones that validate, nominal one that does not, and all
one with the checks of several::
Int = NewType("Int", float)
PositiveInt = NewType("PositiveInt", float)
to_int = Brand[float, Int].make(lambda n: n.is_integer() or f"Expected {n} to be an integer")
positive = Brand[float, Int].check(Filter(lambda n: n > 0, expected="a value greater than 0"))
to_positive_int = Brand[float, PositiveInt].all(to_int, positive)
to_positive_int(1.0) # PositiveInt(1.0)
to_positive_int(-1.5) # raises BrandError
to_positive_int.result(-1.5) # Err(BrandError(...))A predicate passes with True or None; False fails with
"Expected <expected>" and a string fails with that message. Every
filter runs and the messages are joined by newlines, except that an
aborted filter (Filter.abort()) that fails stops the rest.
Classes#
On This Page
Classes