Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] polymorphic type constructor deconstructor
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Max Skaller <skaller@o...>
Subject: Re: [Caml-list] polymorphic type constructor deconstructor
Jeffrey J. Cook wrote:

> Hmmm, I didn't make much sense, I'll try again.


It made sense to me, Felix has such a combinator
for internal use called 'case_arg_n', it
returns the argument of a the n'th kind
of constructor of a union without any run time
checking: there's a function 'check_case'
that checks if a value is a particular case,
so a match on a union type is done by:

if (check_case (1, v)) handler1(case_arg_n(1,v));
else if ...

I guess an implementation dependent C function could
be written which checks Ocaml union tag values.
Still, you'd have to manually magic the return type
for each case ..

OcamlP4 might be able to solve the latter problem
so you could write

type t = Ctor1 ctor1_arg_t | Ctor2 ...

	deconstruct(Ctor1, v)

which would have type ctor_arg_t as desired.
Deconstruct here would call a Camlp4 macro
which looked up a table generated from the
union declaration for t to find the right
argument type, and to calculate the tag index
to pass to the C function for the run time
check.

[I'm assuming in total ignorance the tags are
sequential from the same origin for each union declared]

-- 
John Max Skaller, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners