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: Marcin 'Qrczak' Kowalczyk <qrczak@k...>
Subject: Re: [Caml-list] polymorphic type constructor deconstructor
Dnia sob 7. czerwca 2003 00:37, Jeffrey J. Cook napisa³:

> I would like to simplify this into a function taking a type constructor
> of type 'reply' and a type constructed value of type 'reply' and return
> the contents of the type constructed value, such as (int * int) from
> Foo of int * int. This function would internally have a match statement
> and throw an exception if its not of the right type constructor.

I see no other way than writing these functions manually. Just write them once 
and use them instead of passing constructors.

BTW, the contents of 'REPLY_do_that of successlevel * int' is two things 
separately, not a pair. You can't match against 'Reply_do_that p'. Similarly
for construction.

> The only solution, using this approach, that I can think of, is by passing
> a dummy constructed value, and pattern matching it against the real
> constructed value, throwing an exception if it isn't of the right
> constructor, otherwise decontructing the value and returning the data
> within.

There is no need for dummy values. Just write a matching function for each 
constructor manually. It's not that painful and there is no good other way 
anyway.

> So, is there any way to polymorphically deconstruct a type constructed
> value, which is type safe by providing either a dummy constructed value or
> a dummy constructor?

What type would such a deconstructor have?

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/

-------------------
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