numbas / Numbas

A completely browser-based e-assessment/e-learning system, with an emphasis on mathematics
http://www.numbas.org.uk
Apache License 2.0
199 stars 118 forks source link

Some kind of traits system for data types #744

Open christianp opened 3 years ago

christianp commented 3 years ago

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:

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.

christianp commented 3 years ago

The collection-like data types are another opportunity to use traits: I've wanted "index" and "slice" traits for a long time.