Feature Request: Typedef Comparable

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Feature Request: Typedef Comparable

Mike Stead
Hi List,

I understand there's a Reflect.compare(A, B) which attempts to call the method "__compare(x)" on A, passing it B. This should returns negative int, 0, or positive int depending on whether A is less than, equal to or greater than B.

I'm in a situation where I'd like to create a class which can take a 'Comparable' object (not that uncommon) and was wondering if this could be better expressed as a typedef in StdTypes.


typedef Comparable<T> = {
    function compareTo(value:T):Int

Ref: http://download.oracle.com/javase/6/docs/api/java/lang/Comparable.html

Ideally core types like Float, Int, Date and String would somehow implement this too.

Beyond the benefit of type safety, this would lend itself nicely to sorting arrays of (complex) Comparable objects, i.e.

var teams:Array<Comparable<Team>>;
teams.sort(); // no need to pass sort function.

Anybody see any technical issues with this or have any other thoughts good or bad?