ErrorsStandard library · rule
type.money_split
Money is divided by a count.
Area 19, Standard library; not declinable; the next step is usually the agent's.
Money divided by a count: / by an Int. Dividing money by a count is
sharing it out, and rounding each share loses a minor unit; money.split
hands every one to some share, and money.allocate shares in proportion.
/ by a Decimal stays, since that is a rate (/ 1.20 takes VAT off).
type BillTotal = Money { currency: Currency.gbp min: 0}/// One friend's share of a bill split three ways.function share(total: BillTotal) -> BillTotal { return BillTotal(total / 3)}
error BillTotal is divided by a count
--> main.jadpo:8:22
|8 | return BillTotal(total / 3) | ^^^^^^^^^ money shared out by `/`
- reason
- Dividing money by a count is sharing it out, and rounding each share loses minor units: £10.00 / 3 is £3.33, three times £9.99.
money.splithands every minor unit to some share, and/takes only aDecimalrate, as/ 1.20takes a 20% tax off;3is a whole number. - next
- Share it with
money.split(total, 3), which gives a list of shares adding up to the total, ormoney.allocatefor unequal shares; halve an amount with* 0.5(agent) - rule
- type.
money_ split - help
- https://jadpo.
dev/docs/errors/type. money_ split
Two amounts in currencies both types fix differently don't compile, by
area 3's type.arithmetic_operand (and type.ordering_operand for <):
type CakePrice = Money { currency: Currency.gbp}type EuroPrice = Money { currency: Currency.eur}/// A cake priced in pounds, delivered from a shop that charges in euros.function delivered(price: CakePrice, delivery: EuroPrice) -> CakePrice { return CakePrice(price + delivery)}
error CakePrice and EuroPrice can't be added: they're in different currencies
--> main.jadpo:11:22
|11 | return CakePrice(price + delivery) | ^^^^^^^^^^^^^^^^ GBP and EUR
- reason
CakePriceis in GBP andEuroPricein EUR, as their types or the calls that made them fix; amounts are added only in one currency.- next
- Use two amounts in the same currency (agent)
- rule
- type.
arithmetic_ operand - help
- https://jadpo.
dev/docs/errors/type. arithmetic_ operand
Markdown for agents: /docs/errors/type.money_split.md.Built from Jadpo main at aee2e8f.