On 16/09/2014, at 5:53 AM, john skaller wrote:
> One of the things which has been puzzling me is how to
> pattern match abstract data types.
OMG!
I should document Felix better!!
It's already implemented! Has been for years.
For primitives only, but still ...
/////////////////
ctor A : int = "$1";
ctor int : A = "$1";
var a : A = A(42);
println$ a.int;
fun _match_ctor_Even : A -> bool = "$1%2==0";
fun _match_ctor_Odd : A -> bool = "$1%2==1";
fun _ctor_arg_Even : A -> int = "$1/2";
fun _ctor_arg_Odd : A -> int = "$1/2";
proc decode (a:A) {
match a with
| Even ?x => println$ "Even " + x.str;
| Odd ?x => println$ "Odd " + x.str;
endmatch;
}
decode 42.A;
decode 43.A;
/////////////////
~/felix>flx --test=build/release ta
42
Even 21
Odd 21
////////////////////////////
I don't know if my code handles polymorphic types. But still .. instead of
using isNULL on a pointer .. which is risky ..
match p with
| NULL =>
| nonNULL ?q =>
endmatch
can be implemented!
All I have to do is allow this mechanism for a wider range of types;
at the moment it has to be a nominal type, and an primitive.
[Pattern matching on unions, tuples, records, etc is handled by
the compiler]
In fact, you can do it now, as above, for ANY type. by just giving
it a primitive model (eg A is a primitive model for int, which just
happens to be primitive also but that's irrelevant).
The only problem with the modelling method is it won't nest
so I really do have to expand the allowable range of types
to at least include abstract types (ones created with
type fred = new joe)
--
john skaller
[email protected]
http://felix-lang.org
------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language