On 05/04/2024 10:48, Tim Düsterhus wrote:
Your `Money` example would allow for unsound and/or non-sense
behavior, such as:
$fiveEuros = new Money(5, 'EUR');
$tenDollars = new Money(10, 'EUR');
$what = $fiveEuros + $tenDollars;
What would you expect to be in $what? A `BcMath\Number(15)`?
Yep, since the add method is final and will return a Number then
Number(15) is the only possible result. And even if you did `(new
Money(5, 'EUR')) + (new Money(15, 'EUR'))`, the result would be the
Number 15, not a money. Equally if it was extended to add the isPrime
method as soon as you do any calculation on it you'd get a number
without the isPrime method, so it would be of very limited use. That
could be avoided by returning `static` instead of `self`, but that's
makes addition non-commutative which would be confusing, and using `new
static` sort of requires the constructor to be final, which would make
the Money case impossible.
So yes I'd say you've convinced me that the Number class does need to be
a Final class.