The goal of this tutorial is to show you how you can harness the power of multi-constraint generic type parameters to implement versatile numerical algorithms.
If you write algorithms in a generic way you can reuse them and create instances of the same algorithm parametrized with different types. This way the reuse of your code can be maximized. For numeric applications one obvious need is the ability to chose between
Our first attempt looks somehow like this
This code works perfectly well with any descendant of
This is already pretty powerful and one can for example write
More sophisticated example
If you want to program more sophisticated algorithms you soon run into problems because many algorithms require an order relation, meaning some kind of comparison function like smaller as (<).
The problem is that the class
Now we are able to use the order relations defined in COMPARABLE:
Renaming for multi-constraint generics
But watch out! As some features are inherited twice, like
If you want to be able to create instances of
If you need inversion the following hack can help you to achive your goal: