Hong Phuc
03/16/2025, 5:01 AMGleb Minaev
03/16/2025, 8:34 AMInt
, Long
, and BigInteger
, several kinds of real numbers like Double
and BIgDecimal
, rational numbers, etc.), and for each of them you could have different implementation of operations (standard ones, using different algorithms, using GPU or whatever, etc.). So for each type of numbers N
you could have several implementations of operations. To abstract them, you have to create an interface like MathOperations<N>
(in KMath such interfaces are called Ring
, Algebra
, Field
, and so on). The abstractions are needed when you implement an algorithms that just uses such operations.
For example, I had an algorithm that was abstracted like this and formerly used `Double`s, but didn't work as intended, because 0.0 != -0.0
. Then I switched to rational numbers (that used `Long`s under the hood) by just using another (context) parameter for the algorithm. It does not work well, because `Long`s overflowed. So I wrote rational numbers on `BigInteger`s and without editing my algorithm (!) applied new numbers to it. And now it works fine!
Another thought is about collections. Obviously, you have to define interfaces to abstract several implementations of lists, to abstract several implementations of something iterable, and so on, so that you can write more common (implementation-agnostic) algorithms.
I agree that while you don't have two different implementations of the same "idea" then it's useless to abstract this "idea" as an interface. Extension functions are enough. But the moment you have two of them, you have the use case for introduction of some interface to be able to switch between them.Arjan van Wieringen
03/16/2025, 8:48 AMYoussef Shoaib [MOD]
03/16/2025, 10:18 AMsealed
by default unless I have a reason not to, but that might not be that terribly useful for apps.Wout Werkman
03/17/2025, 8:47 AMHong Phuc
03/17/2025, 8:55 AMRonald Wolvers
03/17/2025, 12:29 PMprotected
fields or methods that could otherwise be accessed, cannot be accessed.
The use case for extension functions I believe is to conveniently be able to "add" methods to a class that one otherwise could not modify, but it does not replace polymorphism nor are they intended to at all I think.