22 May 02:07
automagical record coercions (record subtyping)
At present if you have a function accepting a record with
the set of fields F, you can call it with a record with
the set of fields G, provided F is subset of G, and,
you *explicitly* coerce G to F:
typedef F = (a:int);
typedef G = (a:int, b:int);
fun f(x:F)=>x.a;
var g = (a=1, b=2);
println$ f( g :>> F);
If you leave off the coercion, you will get this:
Client Error binding expression (f g)
CLIENT ERROR
[lookup_name_with_sig] Can't find f of struct {a:(int);b:(int);}
In /Users/johnskaller/felix/./ft.flx: line 5, cols 9 to 10
4: var g = (a=1, b=2);
5: println$ f( g);
**
6:
There's an argument to provide "some kind of subtyping" here:
a G "is an F" (it just has extra fields). The coercion we use has
a common generic name in algebra: "the forgetful functor",
which is a structure preserving map that simply "forgets" some
of its domains structure.
If we could do automagical coercions, you could write "generic"
functions and just call them with any record which extended
(Continue reading)
RSS Feed