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.0 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 99B36BBC1 for ; Thu, 6 Mar 2008 14:18:37 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao8CAKp9z0dIDvb2bWdsb2JhbACQbQwLBAUIChEEgRCSbIZ4 X-IronPort-AV: E=Sophos;i="4.25,455,1199660400"; d="scan'208";a="23439592" Received: from ag-out-0708.google.com ([72.14.246.246]) by mail4-smtp-sop.national.inria.fr with ESMTP; 06 Mar 2008 14:18:36 +0100 Received: by ag-out-0708.google.com with SMTP id 31so3059456agc.3 for ; Thu, 06 Mar 2008 05:18:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:reply-to:to:subject:date:user-agent:cc:references:in-reply-to:content-type:content-transfer-encoding:content-disposition:message-id; bh=By9xeTsjFIG9adLrrPc4J/fetL/WbkLQFNct+xqGJcQ=; b=Hzy8XgHDjL0N4O47qVoiaRKNNNEKwF8Ppmap1/43u/GXinuVWa3hokBaqZpTW3zlg7GG6M1uFWLznpa3fZMKagaJjUHGRZTXxMbB7Kot7FfJYPY4sdvFGcZsFoLJXNnUcuo11p8D0a8ZvuyWzhq2zgrGALuHCy0FyjikqgwQzeo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:reply-to:to:subject:date:user-agent:cc:references:in-reply-to:content-type:content-transfer-encoding:content-disposition:message-id; b=eZeYP53QqTHG/hdXxBJHB9H3LwD+FUhGciR6M3LzlCLIVaDQHA5T8J5uItxFeTO1PzbZs65FqdUbdY2HXjmtHiYSybKArBwfZMShiW6XcG63Xfs/6DHfmfr8ksL8uXmw9y+XxTCxnwSBSQU0dQu3VKGahY2uvA1LeoEiaZ6lO6o= Received: by 10.151.107.18 with SMTP id j18mr1932340ybm.181.1204809514888; Thu, 06 Mar 2008 05:18:34 -0800 (PST) Received: from tama-chan ( [24.99.180.210]) by mx.google.com with ESMTPS id s38sm4414423rnb.10.2008.03.06.05.18.33 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 06 Mar 2008 05:18:34 -0800 (PST) From: Peng Zang Reply-To: peng.zang@gmail.com To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] oo type question Date: Thu, 6 Mar 2008 08:18:29 -0500 User-Agent: KMail/1.9.7 Cc: Michael Wohlwend References: <200803061352.38360.micha-1@fantasymail.de> In-Reply-To: <200803061352.38360.micha-1@fantasymail.de> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200803060818.31857.peng.zang@gmail.com> X-Spam: no; 0.00; hash:01 val:01 mutable:01 subtype:01 val:01 mutable:01 compiler:01 cheers:01 beginner's:01 ocaml:01 bug:01 peng:98 peng:98 polymorphic:01 polymorphic:01 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The problem is that class ['a] store after adding o1 only accepts objects that have both an id method AND a hi method. So to make it work you need to do something like this: let o1 = object method id = 1 method hi = "hi" end;; let o2 = object method id = 2 method ho = "ho" end;; class ['a] store = object val mutable ids = [] method add (o: 'a) = ids <- o#id :: ids end;; let s = new store ;; s#add (o1 :> );; s#add (o2 :> );; Note how I coerce o1 and o2 both to a subtype before adding. Now the problem goes away because to the s#add method o1 and o2 have the same type. BUT, what I think you really want is a polymorphic method, not a polymorphic class. The manual has a decent explanation under polymohrphic methods in the Objects section. So I think you want this: let o1 = object method id = 1 method hi = "hi" end;; let o2 = object method id = 2 method ho = "ho" end;; class store = object val mutable ids = [] method add : 'a. as 'a -> unit = fun o -> ids <- o#id :: ids end;; let s = new store ;; s#add o1;; s#add o2;; Peng On Thursday 06 March 2008 07:52:38 am Michael Wohlwend wrote: > I try to do the following: > > I have some objects (all with an id method): > let o1 = object method id = 1 method hi = "hi" end > let o2 = object method id = 2 method ho = "ho" end > > and now: > > class store = object > val mutable ids = [] > method add o = ids <- o#id :: ids > end > > this doesn't work, since the hidden type variable in the argument to add (I > think). But why can't the compiler just set o to be of type < obj:int; ..> > ?. So I try: > class ['a] store = object > val mutable ids = [] > method add (o: 'a) = ids <- o#id :: ids > end;; > > ...but... > > let s = new store ;; > give the type: > val s : < id : '_a; _.. > store = > > so this doesn't work: > > s#add o1; > s#add o2 > > his expression has type < hi : string; id : int > but is here used with > type < ha : string; id : int > > The second object type has no method ho > > If I define a function, say: > let f o = o#id;; > I get : > val f : < id : 'a; .. > -> 'a = > so this works: > f o1; f o2 > > > why does this not work with methods? > > cheers > Michael > > _______________________________________________ > 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 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.7 (GNU/Linux) iD8DBQFHz+8nfIRcEFL/JewRAm9iAJ9CsOmurw8tI2VUls4noUPN8L0TXgCeN6Zk i0NwtHUdj2XN5lnZbCSbxtw= =MHlA -----END PGP SIGNATURE-----