On 2024-09-18 05:43, Jordan LeDoux wrote:


The problem I was trying to solve involved lots of things that cannot be represented well by primitive types (which is presumably why they are classes in the first place). Things like Complex Numbers, Matrices, or Money. Money can be converted to a float of course (or an int depending on implementation), but Money does not want to be added with something like request count, which might also be an int. Or if it does, it probably wants to know exactly what the context is. There are lots of these kinds of value classes that might be representable with scalars, but would lose a lot of their context and safety if that is done.

Even plain addition with plain numbers can be fraught. Cardinal numbers (1,2,3...) can be added to and subtracted from each other. Ordinal numbers (1st,2nd,3rd...), on the other hand, cannot be added together and subtracting one from another results in an interval, not a number (which means that addition involving ordinal numbers means things like ordinal+interval=ordinal).

Then there are quantities. Quantities of arbitrary dimension (length, duration, monetary value...) can be multiplied, resulting in a quantity of a new dimension, but only quantities of the same dimension can be added.

>
> On the other hand, Money would probably not want to be multiplied with
> other Money values. What would Money squared mean exactly? Things like
> this are very difficult to control for if all you provide is a way to
> control casting to scaar types.
>
While I can't think off the top of my head of a case where money quantities might be multiplied by other money quantities, I can think of situations where one might want to _divide_ them...

Reply via email to