English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    
Browse thread
[Caml-list] recursive type declaration
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: William Lovas <wlovas@s...>
Subject: Re: [Caml-list] recursive type declaration
On Sat, Jun 01, 2002 at 05:26:11PM +0900, Jacques Garrigue wrote:
> From: dengping zhu <zhudp@cs.bu.edu>
> > I can use another way to solve it by define ty1 as 'a ty1. Besides this
> > way, is there any method to solve it? What's more, what's the common way
> > to define such types?
> 
> I believe this is a FAQ.
> 
> Just parameterize one one of the two.
> For instance:
> 
> type 'tc ty1 =
>    | Int of int
>    | String of string
>    | Class of 'tc
> 
> class virtual typeClass =
>    object
>       method virtual typeCon : typeClass ty1
>    end;;

Incidentally, if you really wanted an alternative, you could explicitly
specify the methods you need in ty1's definition, since (i think) class
types are just an abbreviation for objects containing all the necessary
methods.  i.e.:

    type ty1 =
      | Int of int
      | String of string
      | Class of < typeCon : ty1 >

    class virtual typeClass =
      object
        method virtual typeCon : ty1
      end

This is inferior to parametrization in at least two ways:

    1. It most likely fails to capture the intended generalization, making
    the whole thing more error prone, and
    
    2. With a larger example, it could be quite cumbersome (related to 1., 
    in a way).

It's a possibility, but it seems to have a pretty high ugly potential...

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