ErrorsValues and types · rule
type.constructor_input
A type's constructor is given a value it can't convert, because the value is built on a different kind of value or may be none.
Area 2, Values and types; not declinable; the next step is usually the agent's.
Why Jadpo refuses it
A constructor, Type(value), builds a value of a type by checking every
rule on it. It converts only between types built on the same kind of
value, such as two types built on Uuid, so it can check the rules
again without guessing how to change the value. Turning text into a
UUID isn't a check, and Jadpo never casts.
What it looks like
main.jadpo:
type OrderId = Uuid {}type ProductName = Text { max_length: 120}function order_for(name: ProductName) -> OrderId { return OrderId(name)}
error OrderId can't be built from ProductName
--> main.jadpo:8:20
|8 | return OrderId(name) | ^^^^ this is `ProductName`
- reason
OrderIdis built onUuidandProductNameis built onText; a constructor converts only between types with the same representation, re-checking the rules.- next
- Pass a value built on
Uuid(agent) - rule
- type.
constructor_ input - help
- https://jadpo.
dev/docs/errors/type. constructor_ input
(agent) at the end of the next: line means your coding agent can make
the fix itself.
A value that may be none builds nothing either. Here maybe is a
Count?, and a Quantity can't be none:
type Count = Int { min: 0}type Quantity = Int { min: 1 max: 1000}function quantity_of(maybe: Count?) -> Quantity { return Quantity(maybe)}
error Quantity can't be built from Count?
--> main.jadpo:11:21
|11 | return Quantity(maybe) | ^^^^^ this is `Count?`
- reason
Count?may benone, and aQuantitycan't be.- next
- Use
matchto handlenonebefore building aQuantity(agent) - rule
- type.
constructor_ input - help
- https://jadpo.
dev/docs/errors/type. constructor_ input
Handle the none case first with match. The value inside some(...)
is a Count, built on Int as Quantity is, so a Quantity can be
built from it.
How to fix it
If the function should start from another id, take a value built on
Uuid, such as a CartId:
type CartId = Uuid {}function order_for(cart: CartId) -> OrderId { return OrderId(cart)}
OrderId(cart) now converts one UUID-based type to another on purpose,
and the two stay separate types everywhere else.
Related
Markdown for agents: /docs/errors/type.constructor_input.md.Built from Jadpo main at aee2e8f.