Version française
Home     About     Download     Resources     Contact us    
Browse thread
question about polymorphic methods
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Julien Moutinho <julien.moutinho@g...>
Subject: Re: [Caml-list] question about polymorphic methods
On Sun, Sep 23, 2007 at 07:11:21PM -0700, Warren Harris wrote:
> I have a simple output stream class that abstracts over out_channels and 
> buffers. I would like to add a printf method that takes a format directive, 
> and calls the appropriate Printf function.
> [...]
> This type parameter propagates through numerous places in my code,
> in some cases requiring other methods to become polymorphic.

Consider using the class below instead of out_stream directly:

class my'out_stream och buf =
  object
    val buffer = new out_stream_of_buffer buf
    method buffer = buffer
    val outchan = new out_stream_of_channel och
    method outchan = outchan
  end

Or this one, depending on how you get your buf and och:

class my'out_stream'opt
  ?och ?buf () =
  object (self)
    val mutable buffer = None
    method init_buffer buf =
        buffer <- Some (new out_stream_of_buffer buf)
    initializer
        match buf with None -> ()
        | Some buf -> self#init_buffer buf
    method buffer =
        match buffer with
        | Some o -> o
        | None -> failwith "MyStream.my'out_stream'opt#buf: no buffer provided"
    
    val mutable outchan = None
    method init_outchan och =
        outchan <- Some (new out_stream_of_channel och)
    initializer
        match och with None -> ()
        | Some och -> self#init_outchan och
    method outchan =
        match outchan with
        | Some o -> o
        | None -> failwith "MyStream.my'out_stream'opt#och: no channel provided"
  end

HTH,
  Julien.