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.2 required=5.0 tests=AWL,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 C3F81BC69 for ; Mon, 24 Sep 2007 14:23:22 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAIpI90ZC+Vypn2dsb2JhbACOGQIBAQcEBgcIGA X-IronPort-AV: E=Sophos;i="4.20,291,1186351200"; d="scan'208";a="16691751" Received: from ug-out-1314.google.com ([66.249.92.169]) by mail4-smtp-sop.national.inria.fr with ESMTP; 24 Sep 2007 14:23:22 +0200 Received: by ug-out-1314.google.com with SMTP id m3so773699ugc for ; Mon, 24 Sep 2007 05:23:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:received:date:to:subject:message-id:references:mime-version:content-type:content-disposition:in-reply-to:user-agent:from; bh=XTA8bcXUgJ9kpdxCGmwnRDVvpyg4hHPeaFTqwp4cC3c=; b=Dwjwx/Nc6s4wKDVRMk17VZ1OOe8lfk5qLGiwIL55T+kPuVM5L6wu0zuJUgvibL6GdU7EvS3tX8N7GB/XR9+qOcpHPMSSEOCXd9I2W2/M/l9J72qH4jBgAN/lNPg7/K+ZxioKcBCYi+7+8tBfoxI9HAHFqu/nc0sljdg9hYu+X5w= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:date:to:subject:message-id:references:mime-version:content-type:content-disposition:in-reply-to:user-agent:from; b=Ff2iZp1DagqYLlyLOftK5BmQmFV2V1mlpR3RYTVcy1PWWqPq+ji0Gsw7HHgzjo7p3KmT4Ck5Kw9QdknKvxwbx7gmCSlf4GqXvehAqkPCTAZdW+F9EBRb4WlzBgHfYWYY3NjjYWcvYztHA5aEiJwS8KISUImbZOsjpYfF+Pz8Ls4= Received: by 10.67.119.15 with SMTP id w15mr7723512ugm.1190636601409; Mon, 24 Sep 2007 05:23:21 -0700 (PDT) Received: from localhost ( [82.122.118.71]) by mx.google.com with ESMTPS id j1sm8257173ugf.2007.09.24.05.23.17 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 24 Sep 2007 05:23:19 -0700 (PDT) Received: by localhost (sSMTP sendmail emulation); Mon, 24 Sep 2007 14:23:41 +0200 Date: Mon, 24 Sep 2007 14:23:41 +0200 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] question about polymorphic methods Message-ID: <20070924122341.GB4663@localhost> References: <46F71CC9.8090609@liveops.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <46F71CC9.8090609@liveops.com> User-Agent: Mutt/1.5.16 (2007-06-11) From: Julien Moutinho 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 23,:98 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.