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.1 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 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 0184ABC69 for ; Tue, 13 Nov 2007 19:29:11 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAGN5OUfAXQImh2dsb2JhbACPBgEBAQgKKQ X-IronPort-AV: E=Sophos;i="4.21,411,1188770400"; d="scan'208";a="19242819" Received: from discorde.inria.fr ([192.93.2.38]) by mail4-smtp-sop.national.inria.fr with ESMTP; 13 Nov 2007 19:29:11 +0100 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id lADIT7P5009719 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 13 Nov 2007 19:29:11 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAFR6OUdC+VyomGdsb2JhbACPBgEBAQEHAggn X-IronPort-AV: E=Sophos;i="4.21,411,1188770400"; d="scan'208";a="5753994" Received: from ug-out-1314.google.com ([66.249.92.168]) by mail3-smtp-sop.national.inria.fr with ESMTP; 13 Nov 2007 19:29:11 +0100 Received: by ug-out-1314.google.com with SMTP id m3so159587ugc for ; Tue, 13 Nov 2007 10:29:10 -0800 (PST) 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:mail-followup-to:references:mime-version:content-type:content-disposition:in-reply-to:user-agent:from; bh=Otclo7PhJotc43piumtt4xCnowvPvxdhZZ6lBH827VE=; b=I910hcRyeDyuqdlhcykWY4/kGFaLQoCUwWuGSAYPAJ2pch6rFfzVeKaTTyOR18PycFo5p4hkllqD5BTH60KYzpd5dVHVImFudBHODWG20HLBp8euMr0XOhD4VIYsSUQNCPMgBDc6xymZlneGMg+7yimUEAmLqpH2+xulzUr5D6E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:date:to:subject:message-id:mail-followup-to:references:mime-version:content-type:content-disposition:in-reply-to:user-agent:from; b=ePnZoNk1UXBshdwLt493EG4yX8JGDzQDoV0oSw+Nq53U0T1MOUpEDmlTZE8h04/QUiY4j7Bo7s6PnPgkKvg7p9VgB3hCjcmACxkY4gXmHHcidGZEdYXHsynEkrHW25dk1BHmK39JuKYtWvwJoYDykD7KyDcvru3+1jK+NsKP6/o= Received: by 10.67.116.4 with SMTP id t4mr851564ugm.1194978550045; Tue, 13 Nov 2007 10:29:10 -0800 (PST) Received: from localhost ( [82.122.104.65]) by mx.google.com with ESMTPS id l33sm530251ugc.2007.11.13.10.29.06 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 13 Nov 2007 10:29:08 -0800 (PST) Received: by localhost (sSMTP sendmail emulation); Tue, 13 Nov 2007 19:29:55 +0100 Date: Tue, 13 Nov 2007 19:29:55 +0100 To: caml-list@inria.fr Subject: Re: [Caml-list] polymorphic lists, existential types and asorted other hattery Message-ID: <20071113182955.GB14227@localhost> Mail-Followup-To: Julien Moutinho , caml-list@inria.fr References: <200711131227.18910.peng.zang@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <200711131227.18910.peng.zang@gmail.com> User-Agent: Mutt/1.5.17 (2007-11-01) From: Julien Moutinho X-Miltered: at discorde with ID 4739ECF3.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; existential:01 val:01 mutable:01 iter:01 endline:01 ocaml:01 ocamlc:01 val:01 mutable:01 peng:98 2.0:98 soleil:98 soleil:98 polymorphic:01 wrote:01 On Tue, Nov 13, 2007 at 12:27:07PM -0500, Peng Zang wrote: > Is there a way to create lists in which the elements may be of > differing types but which all have some set of operations defined > (eg. tostr) in common? > [...] Objects may be of interest to you: % cat file.ml class ['e] skel (e: 'e) = object val mutable e = e method get = e method set x = e <- x end class virtual ['e] xs (to_string: 'e -> string) = object (self) method virtual get : 'e method coerce = (self :> < show : string >) method show = to_string self#get end class oint (e: 'e) = object inherit ['e] skel e inherit ['e] xs string_of_int end class ofloat (e: 'e) = object inherit ['e] skel e inherit ['e] xs string_of_float end let xs = [ (new oint 1)#coerce ; (new ofloat 2.0)#coerce ; (new oint 3 :> < show : string >) ; (object method show = "soleil" end) ] ;; List.iter (fun o -> print_endline o#show) xs % ocaml file.ml 1 2. 3 soleil % ocamlc -i file.ml class ['a] skel : 'a -> object val mutable e : 'a method get : 'a method set : 'a -> unit end class virtual ['a] xs : ('a -> string) -> object method coerce : < show : string > method virtual get : 'a method show : string end class oint : int -> object val mutable e : int method coerce : < show : string > method get : int method set : int -> unit method show : string end class ofloat : float -> object val mutable e : float method coerce : < show : string > method get : float method set : float -> unit method show : string end val xs : < show : string > list HTH, Julien.