Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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

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

John Max Skaller,
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: