Hi Terje, I think both of your examples are compatible with the idea of grouped operators, as long as we don't constrain a type to implement all operators with the same right-hand side.
On Mon, 2 Mar 2020 at 17:23, Terje Slettebø < [email protected]> wrote: > > Adding and subtracting money makes sense, but it makes no sense to > multiply > or divide them, and this is just *one* domain. > This is true, but it does make sense to multiply and divide them by numbers, for instance $totalPrice = $unitPrice * $quantity In general, if $a + $a represents addition, then $a * 2 should logically give the same result. As has been raised, there is an argument for keeping / separate, for types that cannot handle fractional operations; it would be a matter of choice whether $price / 2 should be supported because it would lead to fractional pennies/cents. > $total_discount = $total * $discount; > > Here, $total_discount and $total is Money, while $discount is a > Percentage, > so we should be able to define a method that allows you to multiply Money > with Percentage, and return a Money object. Adding Money and Percentage > would > make no sense, but multiplying them does. > If this operator was implemented in isolation, it would again be drifting into domain-specific language territory. (I don't personally have a problem with that, but many people vocally object to it.) However, it could easily be made part of a consistent set of arithmetic operators: On class Money: Money + Money => Money Money - Money => Money Money * int => Money Money / int => Money int * Money => Money int / Money => Error Money * Percentage => Money Money / Percentage => Money Percentage * Money => Money Percentage / Money => Error Optionally, on class Percentage: Percentage + Percentage => Percentage Percentage - Percentage => Percentage Percentage* int => Percentage Percentage / int => Percentage Regards, -- Rowan Tommins [IMSoP]
