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=1.4 required=5.0 tests=SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id EE25ABC69 for ; Tue, 25 Sep 2007 03:25:04 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAIcA+EZA6bjnnmdsb2JhbACOGwIBAQcEBg8Y X-IronPort-AV: E=Sophos;i="4.20,293,1186351200"; d="scan'208";a="16725115" Received: from wr-out-0506.google.com ([64.233.184.231]) by mail4-smtp-sop.national.inria.fr with ESMTP; 25 Sep 2007 03:25:04 +0200 Received: by wr-out-0506.google.com with SMTP id c8so574149wra for ; Mon, 24 Sep 2007 18:25:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; bh=IgCJnyKfUWWth66m70WMnWc1hwu3E06Gj1md4M3mWQg=; b=ToI5mVxnGbkl2i7Ne4OvDE3h9T9NR9WbnXcCfRRKK/gehkxdZ8apTmt1WO1rVpd2jHiqFdBP01ycfiJQfQraXz0VhS6sEREw40ogXwF3i1JJ8/JnsmpOCgPleviu78rzOERlgRqbQuOipJxGkO8UnskDc1YeMI4fwHRsG9ghFNc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=EXWGolaZ18jzkbAb2rxOs8yZbvYKxVuLtzNtW9OjB8c+g7zV2NjVpWZ0/tBlgurwUPnr+hSakXwoEN0/6JtETsLuGqILW1aiCBgjSQWm/51Pnn901zaT5ohXunnB2A9vaUCs1IijrqXYvez9e9gqYliOxqPG/XwNPlj3qomQHvM= Received: by 10.142.216.9 with SMTP id o9mr1187573wfg.1190683501477; Mon, 24 Sep 2007 18:25:01 -0700 (PDT) Received: by 10.143.30.17 with HTTP; Mon, 24 Sep 2007 18:25:01 -0700 (PDT) Message-ID: <9d3ec8300709241825n4c1d0916n7a6512b2100a5272@mail.gmail.com> Date: Tue, 25 Sep 2007 03:25:01 +0200 From: "Till Varoquaux" Sender: till.varoquaux@gmail.com To: "Warren Harris" Subject: Re: [Caml-list] question about polymorphic methods (caml: to exclusive) Cc: caml-list@yquem.inria.fr In-Reply-To: <46F84C35.9030903@liveops.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <46F71CC9.8090609@liveops.com> <20070924122341.GB4663@localhost> <46F84C35.9030903@liveops.com> X-Google-Sender-Auth: 819698fbc507bf98 X-Spam: no; 0.00; ocaml's:01 stdlib:01 printf:01 buffer:01 buf:01 buffer:01 buf:01 printf:01 cheers:01 val:01 val:01 outchan:01 outchan:01 mutable:01 failwith:01 It appears to me as though the second argument to the format type is used only internally in ocaml's stdlib (by mkprintf for instance) and that it should not be exposed the way it is. I do not believe you have a simple way to get rid of it without resorting to some magic (yikes): class type out_stream = object method print : string -> unit method printf : 'a . ('a,unit,unit) format -> 'a method flush : unit end class out_stream_of_buffer buf = object (self) method print str = Buffer.add_string buf str method printf : 'a . ('a,unit,unit) format -> 'a = fun fmt -> Printf.bprintf buf (Obj.magic fmt) method flush = () end .. There is some black (Obj.)magic going here (Printf is very magical anyways) so you can expect troubles (unless the second parameter is really useless). Cheers, Till On 9/25/07, Warren Harris wrote: > > 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 > > > > _______________________________________________ > 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 > > -- http://till-varoquaux.blogspot.com/