Values and types · rule
type.unit_as_number
You declared a type named for a unit, such as Grams, Pence or Percent, on a plain number.
Area 2, Values and types; declinable; the next step is usually the agent's.
Why Jadpo refuses it
A type built on Int or Decimal holds a number and nothing else, so
its name is the only place the unit is written. A value in kilograms
passes for one in grams, a price in pounds for one in pence, and the
number adds to any other. The standard types keep the unit with the
number: a Mass converts between units exactly and shows itself in the
one you ask for, and a Money knows its currency.
Jadpo goes by the type's whole name, so it can be wrong: a name can be a unit by chance. That is a judgement call, so you can decline it with a reason, which review reads.
What it looks like
main.jadpo:
/// What a parcel weighs.type Grams = Int { min: 1 max: 30000}
error Grams holds a Mass as a plain number
--> main.jadpo:2:6
|2 | type Grams = Int { | ^^^^^ named for a unit
- reason
Gramsis named for a unit ofMassbut is a plainInt, so nothing keeps the unit with the number: a value in another unit passes for one in this one, and it adds to any other number. AMasscarries what the number means, and converts and shows itself by its own rules.- next
- Build it on
Mass, named for what it holds rather than its unit, or writedecline type.unit_as_number: "<why>"above it if this is deliberate (agent) - rule
- type.
unit_ as_ number - help
- https://jadpo.
dev/docs/errors/type. unit_ as_ number
(agent) at the end of the next: line means your coding agent can make
the fix itself.
A currency's minor unit is Money held as a whole number, so a price in
pence is refused too.
main.jadpo:
/// What a loaf costs.type Pence = Int { min: 0}
error Pence holds a Money as a plain number
--> main.jadpo:2:6
|2 | type Pence = Int { | ^^^^^ named for a unit
- reason
Penceis named for a unit ofMoneybut is a plainInt, so nothing keeps the unit with the number: a value in another unit passes for one in this one, and it adds to any other number. AMoneycarries what the number means, and converts and shows itself by its own rules.- next
- Build it on
Money, named for what it holds rather than its unit, or writedecline type.unit_as_number: "<why>"above it if this is deliberate (agent) - rule
- type.
unit_ as_ number - help
- https://jadpo.
dev/docs/errors/type. unit_ as_ number
How to fix it
The question is whether the type holds what its name says.
If it holds that unit's kind of value, build it on the standard type and name it for what it holds. main.jadpo becomes:
/// What a parcel weighs.type ParcelWeight = Mass { min: 1g max: 30kg}
A value now names its unit when it is made, as 250g or
units.of(grams, MassUnit.gram), and a bare number where a ParcelWeight
is expected is type.unit_required. A price is the same with Money:
/// What a loaf costs.type LoafPrice = Money { currency: Currency.gbp min: 0}
A LoafPrice is made from pence with money.from_minor, and adding it
to a price in another currency doesn't compile.
If the name is a unit by chance, keep the type, and write a decline line with the reason directly above the declaration:
/// Knots the scouts can tie.decline type.unit_as_number: "Knots the scouts can tie, not a speed"type Knots = Int { min: 0}
jadpo approval lists every decline with its reason, so a reviewer sees
why it isn't the standard type.
Related
Markdown for agents: /docs/errors/type.unit_as_number.md.Built from Jadpo main at e00e61f.