Re: question on recursive types

From: Jerome Vouillon (Jerome.Vouillon@inria.fr)
Date: Mon Mar 22 1999 - 13:53:18 MET


Date: Mon, 22 Mar 1999 13:53:18 +0100
From: Jerome Vouillon <Jerome.Vouillon@inria.fr>
To: Markus Mottl <mottl@miss.wu-wien.ac.at>, OCAML <caml-list@inria.fr>
Subject: Re: question on recursive types
In-Reply-To: <199903211947.UAA20606@miss.wu-wien.ac.at>; from Markus Mottl on Sun, Mar 21, 1999 at 08:47:55PM +0100

Hello,

On Sun, Mar 21, 1999 at 08:47:55PM +0100, Markus Mottl wrote:
> I am trying to find a useful approach for sending streams of messages
> to objects and have come across the following problem (code example):
>
> class transformer = object (self)
> method apply_strm s =
> try while true do (Stream.next s) self s done with Stream.Failure -> ()
> end
[...]
> Unfortunately, this results in a recursive type, because the elements of
> the stream are functions that have to accept the same type of streams
> as parameter.
[...]

The usual trick to avoid explicitely recursive types is to use a type
constructor:

    type 'a t = Stream of ('a -> 'a t -> unit) Stream.t;;
    class transformer = object (self)
      method apply_strm s =
        try
          while true do (Stream.next s) self (Stream s) done
        with Stream.Failure -> ()
    end;;

-- Jérôme



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:21 MET