From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL,HTML_MESSAGE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id D43EABC69 for ; Tue, 25 Sep 2007 01:46:00 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAGTo90bRnZoih2dsb2JhbACCOzWLKwIBCAonmB4 X-IronPort-AV: E=Sophos;i="4.20,293,1186351200"; d="scan'208,217";a="3197918" Received: from mail-pai.liveops.com (HELO mail.liveops.com) ([209.157.154.34]) by mail3-smtp-sop.national.inria.fr with ESMTP; 25 Sep 2007 01:46:00 +0200 Received: from whelm.local ([192.168.33.27]) by mail.liveops.com with Microsoft SMTPSVC(6.0.3790.3959); Mon, 24 Sep 2007 16:47:08 -0700 Message-ID: <46F84C35.9030903@liveops.com> Date: Mon, 24 Sep 2007 16:45:57 -0700 From: Warren Harris User-Agent: Thunderbird 2.0.0.6 (Macintosh/20070728) MIME-Version: 1.0 To: "Julien Moutinho - julien.moutinho@gmail.com" <+caml+warren+b1373cd4e6.julien.moutinho#gmail.com@spamgourmet.com> Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] question about polymorphic methods (caml: to exclusive) References: <46F71CC9.8090609@liveops.com> <20070924122341.GB4663@localhost> In-Reply-To: <20070924122341.GB4663@localhost> Content-Type: multipart/alternative; boundary="------------010005030408060608060209" X-OriginalArrivalTime: 24 Sep 2007 23:47:08.0514 (UTC) FILETIME=[3823D420:01C7FF05] X-Spam: no; 0.00; printf:01 printf:01 buf:01 val:01 buffer:01 buffer:01 buf:01 val:01 outchan:01 outchan:01 mutable:01 failwith:01 mutable:01 failwith:01 beginner's:01 This is a multi-part message in MIME format. --------------010005030408060608060209 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Julien, Thanks for your suggestion. I realize that I can paper over the problem in various ways, but I was hoping for a more type-theoretic approach to solving the problem of needing an implementation-specific but otherwise abstract type parameter in printf's signature. If I could declare 'b as a function of the particular implementation in question, that might solve it, but I'm not sure how to express this. Warren Julien Moutinho - julien.moutinho@gmail.com wrote: > 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. > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > --------------010005030408060608060209 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Julien, Thanks for your suggestion. I realize that I can paper over the problem in various ways, but I was hoping for a more type-theoretic approach to solving the problem of needing an implementation-specific but otherwise abstract type parameter in printf's signature. If I could declare 'b as a function of the particular implementation in question, that might solve it, but I'm not sure how to express this.

Warren


Julien Moutinho - julien.moutinho@gmail.com wrote:
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.

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

  
--------------010005030408060608060209--