RFR: [type-classes] Initial work to enable type-classes mediated operator support
Maurizio Cimadamore
mcimadamore at openjdk.org
Thu Jan 22 18:32:33 UTC 2026
This PR adds experimental support for type-class based operator resolution. This means that it is possible to define classes that "witness" some numerical type class, like so:
record Box(int i) {
__witness Integral<Box> INTEGRAL = ...
}
Then, we can start using `Box` in e.g. binary operations, like so:
Box one = new Box(1);
Box two = new Box(1);
one + two // three
In order to support type-class mediated operator resolution, we needed to add a new kind of operator helper in `Operators`. These new operator helpers (one for unary operators, one for binary operators) wrap some method in one of the standard numeric classes -- for instance, type class `+` is realized through `Numerical::add`, and so forth.
There's a new kind of operator symbol, namely `TypeClassOperatorSymbol` which is used to store the type of the operation as well as the method symbol in charge of performing the operation.
To resolve an operator, we basically have to prove that a witness for the corresponding class, parameterized by the operand type exists. E.g. in the above, we need to find a witness for `Numerical<Box>`.
During lowering, we emit such witness, and call the correct operator method on it.
Note that support for assignment operators is also provided -- `Lower` already has logic to lower some assignment operators into plain binary operations, so we just leverage that support to translate type-class based assignment ops.
-------------
Commit messages:
- Minor fixes
- Merge branch 'type-classes' into op_overloading
- Shuffle comment table in Operators
- Cleanup code
- Merge branch 'op_overloading' into joe_type_class_hierarchy
- Initial push
- Appease jcheck some more.
- Appease jcheck
- 8338529: Initial iteration of numerics modeling interfaces
Changes: https://git.openjdk.org/valhalla/pull/1948/files
Webrev: https://webrevs.openjdk.org/?repo=valhalla&pr=1948&range=00
Stats: 563 lines in 10 files changed: 504 ins; 2 del; 57 mod
Patch: https://git.openjdk.org/valhalla/pull/1948.diff
Fetch: git fetch https://git.openjdk.org/valhalla.git pull/1948/head:pull/1948
PR: https://git.openjdk.org/valhalla/pull/1948
More information about the valhalla-dev
mailing list