I've recently had to implement min and max for each of the number-like data types. There are other functions which rely on min and max, such as clamp.
It would be nice to have a way of recording implementations of traits such as orderability and equality, so that other functions can easily use the appropriate method.
At the moment, we have Numbas.util.equalityTests, which contains functions for determining if two tokens of the same type are equal.
I suppose you could evaluate a JME expression with the tokens subbed in, and let the type-checker do the work, but that feels like it adds a lot of overhead.
A sketch of a system:
A function Numbas.util.register_trait_implementation( method, type, fn) which registers a function implementing the given method for a particular type.
A function Numbas.util.get_trait_implementation( method, type) which returns a function implementing the given method for a particular type.
What would be really clever would to be able to derive traits, e.g. if you have <= and >=, you can get =, < and > by combining them.
I've recently had to implement
min
andmax
for each of the number-like data types. There are other functions which rely onmin
andmax
, such asclamp
.It would be nice to have a way of recording implementations of traits such as orderability and equality, so that other functions can easily use the appropriate method.
At the moment, we have
Numbas.util.equalityTests
, which contains functions for determining if two tokens of the same type are equal.I suppose you could evaluate a JME expression with the tokens subbed in, and let the type-checker do the work, but that feels like it adds a lot of overhead.
A sketch of a system:
Numbas.util.register_trait_implementation( method, type, fn)
which registers a function implementing the given method for a particular type.Numbas.util.get_trait_implementation( method, type)
which returns a function implementing the given method for a particular type.What would be really clever would to be able to derive traits, e.g. if you have
<=
and>=
, you can get=
,<
and>
by combining them.