Open rorystephenson opened 1 year ago
If you agree with the proposed change I would be happy to draft a PR.
The Point
class is generally bad, I'd love an update on it.
It probably shouldn't have any multiplication at all, it's a point, not a vector.
Its attempt to be generic over T extends num
is a failure. It should just have subclasses for integer points, double points, and maybe num points.
Fixing just *
, by making it an extension method, could apply to every method, but really requires the class to be final
, otherwise we could be breaking subclasses. (And until all code is 3.0 and respects final
, that's tricky.)
And still only works if all users import the library.
Making it an extension type sounds more promising.
Its attempt to be generic over
T extends num
is failure. It should just have subclasses for integer points, double points, and maybe num points.Fixing just
*
, by making it an extension method, could apply to every method, but really requires the class to befinal
, otherwise we could be breaking subclasses. (And until all code is 3.0 and respectsfinal
, that's tricky.) And still only works if all users import the library.Making it an extension type sounds more promising.
@lrhn Thanks for the quick response. It's not clear to me what you believe is the best approach? When you say an extension type
does that mean defining extensions as I have done or something else?
An extension type is a new feature in development, which allows creating a new type with a different view on an existing type.
Change Intent
Remove
Point
's*
operator method and add extensions toPoint
which define*
such that only runtime-safe multiplications are possible. This would look something like:Justification
The
*
operator onPoint<T extends num>
allows multiplying by anum
factor. This was added as a convenience method to allow multiplying aPoint<double>
with anint
factor (this is documented on the method). The issue is that multiplying aPoint<int>
by adouble
factor will cause a runtime error.This proposed change would still allow multiplying
Point<double>
andPoint<num>
withnum
whilst preventingPoint<int>
multiplication withnum
ordouble
and still allowingPoint<int>
multiplication withint
.Impact
Code that multiplies a Point by a double factor will now trigger the following compile-time error (flagged by the analyzer):
A value of type 'double' can't be assigned to a variable of type 'int'.
Mitigation
Adding the extension methods allows correct use of
*
whilst preventingPoint<int> * double
. I'm not sure what else can be done to mitigate the impact.