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 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 0A40BBBC1 for ; Thu, 6 Mar 2008 17:58:27 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAACqxz0dA6bjokGdsb2JhbACQeAEBAQEHBAQJChEHgQ2TAIZ+ X-IronPort-AV: E=Sophos;i="4.25,456,1199660400"; d="scan'208";a="23449993" Received: from wr-out-0506.google.com ([64.233.184.232]) by mail4-smtp-sop.national.inria.fr with ESMTP; 06 Mar 2008 17:58:26 +0100 Received: by wr-out-0506.google.com with SMTP id 67so9441679wri.3 for ; Thu, 06 Mar 2008 08:58:25 -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=lEFBLaisHJet9zLqdX1bLh3IyEo1/pQjOdos7d66YMM=; b=J2AwjGK71S4yd5D+Fuxegwdr9xd6x6SliOQJWbIiEl6W2YdXBAqGR7XaHCwPW/zbqYv9bb8U51tQA9ZXrSBhSU2KoLez7mpaD4MxHa0OhxGyXIYK6c2pGweplXDbDW3elZCtfvKsusmcs5LfLL4lcNTo3xnIx0JrRr7rjLvprx8= 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=QFp3p446og3Jnsflzxv/ljcgAPH8BrWxbSRSJYC6Sp6ywhwp6GF/2pINXL9zOHD1F4RtBkj9n5iDdAf1Xv0VqRACwCeZxrpP06OwMUIUwzvaxaAXUSG1E2YiO4SoPxFE4fr+58qq6jsiYmJYTKRxkBlU8jGr3SLAyZEiFLOnaTE= Received: by 10.141.171.6 with SMTP id y6mr11356rvo.84.1204822704118; Thu, 06 Mar 2008 08:58:24 -0800 (PST) Received: from tama-chan ( [24.99.180.210]) by mx.google.com with ESMTPS id i52sm4848809rne.9.2008.03.06.08.58.16 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 06 Mar 2008 08:58:17 -0800 (PST) From: Peng Zang Reply-To: peng.zang@gmail.com To: Michael Wohlwend Subject: Re: [Caml-list] oo type question Date: Thu, 6 Mar 2008 11:58:12 -0500 User-Agent: KMail/1.9.7 Cc: caml-list@yquem.inria.fr References: <200803061352.38360.micha-1@fantasymail.de> <200803060818.31857.peng.zang@gmail.com> <200803061645.30224.micha-1@fantasymail.de> In-Reply-To: <200803061645.30224.micha-1@fantasymail.de> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200803061158.14882.peng.zang@gmail.com> X-Spam: no; 0.00; hash:01 donnerstag:01 subtype:01 casts:01 someclass:01 hmmm:01 iirc:01 undecidable:01 coercions:01 peng:98 peng:98 polymorphic:01 polymorphic:01 wrote:01 caml-list:01 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 06 March 2008 10:45:30 am Michael Wohlwend wrote: > Am Donnerstag, 6. März 2008 14:18:29 schrieb Peng Zang: > > 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. > > yes, that's the easy way, but then I have to do casts all over the place, > not very nice > > > 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: > > method add : 'a. as 'a -> unit = > > fun o -> ids <- o#id :: ids > > I know that version and of course it works. I only didn't understand (and > still do not) why it's not possible to just declare > method add (a: #someClass) ... which seems natural for me. And it works for > normal functions that way. > > If you have many classes (actually it's for adding widgets to group > widgets) it seems just a bit too complicated and not a natural way to > declare such a simple thing. Hmmm... I do not know of another easy way to do this. Polymorphic methods cannot be inferred in general because IIRC it is undecidable? That said, this special case seems simple enough. Other (possibly lame) suggestions: 1) You can give some nice type alias to " as 'a" so the type specification looks simpler 2) You can go with the polymorphic class case and write a small helper function, eg: let addwithcoerce s a = s#add (a :> ) to hide the coercions. Perhaps others on this list will have additional ideas too. Regards, Peng -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.7 (GNU/Linux) iD8DBQFH0CKmfIRcEFL/JewRAkLXAKCLog1BC8wViOJ+yF/TjoG4T+5KDACgxXJ1 HiUc6i+D9bvpKWn/zCbWEf0= =R6fR -----END PGP SIGNATURE-----