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_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id D4091BC6B for ; Thu, 18 Oct 2007 18:11:01 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAANIiF0fAXQInh2dsb2JhbACOTQIBCAop X-IronPort-AV: E=Sophos;i="4.21,296,1188770400"; d="scan'208";a="3214224" Received: from concorde.inria.fr ([192.93.2.39]) by mail1-smtp-roc.national.inria.fr with ESMTP; 18 Oct 2007 18:11:01 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l9IGAt92025179 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 18 Oct 2007 18:11:01 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAANIiF0dQW+UCh2dsb2JhbACOTQIBCAop X-IronPort-AV: E=Sophos;i="4.21,296,1188770400"; d="scan'208";a="3214218" Received: from main.gmane.org (HELO ciao.gmane.org) ([80.91.229.2]) by mail1-smtp-roc.national.inria.fr with ESMTP; 18 Oct 2007 18:11:00 +0200 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1IiXxV-0001x5-Fw for caml-list@inria.fr; Thu, 18 Oct 2007 16:10:53 +0000 Received: from ivr94-8-88-162-26-239.fbx.proxad.net ([88.162.26.239]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 18 Oct 2007 16:10:53 +0000 Received: from li by ivr94-8-88-162-26-239.fbx.proxad.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 18 Oct 2007 16:10:53 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Zheng Li Subject: Re: Smells like duck-typing Date: Thu, 18 Oct 2007 18:13:05 +0200 Message-ID: <877ilkwgdq.fsf@pps.jussieu.fr> References: <932096.75090.qm@web54602.mail.re2.yahoo.com> <877ill225i.fsf@pps.jussieu.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ivr94-8-88-162-26-239.fbx.proxad.net User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.50 (gnu/linux) Cancel-Lock: sha1:mKwsWN4aTvx78oez16mASvBb3xs= Sender: news X-Miltered: at concorde with ID 4717858F.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; val:01 val:01 bug:01 printf:01 printf:01 ocaml:01 beginner's:01 ocaml:01 bug:01 beginners:01 pps:01 pps:01 caml-list:01 caml-list:01 inherit:01 Yet another. ------------------------------------------------------------------------------ class type blurb = object method id: int method title:string method intro:string end class type fresh = object method title:string method intro:string method body:string end class type full = object inherit blurb inherit fresh end class virtual templt = object val virtual id: int val virtual title: string val virtual intro: string val virtual body: string method id = id method title = title method intro = intro method body = body end let blurb (i, t, it) :> blurb = object inherit templt val id=i val title=t val intro=it end let fresh (t, it, b) :> fresh = object inherit templt val title=t val intro=it val body=b end ------------------------------------------------------------------------------ Strangely though, there seems to be a bug in the OO type system: (The solution proposed above is safe, as it does coercion) # let coredump = object inherit templt end;; val coredump : templt = # coredump#title;; Process caml-toplevel segmentation fault Zheng Li writes: > Hi, > > I would vote for the object solution. However, both record and object have > their own pros and cons. The actual solution should depends on your requirement > and taste. Just for an example, the following data structure is a possibility: > > # type +'a t = {title:string; intro:string; extra:'a} constraint 'a = < .. >;; > # let fresh (title,intro,(body:string)) = > {title=title; intro=intro; extra=object method body=body end};; > # let blurb ((id:int),title,intro) = > {title=title; intro=intro; extra=object method id=id end};; > # let print_metadata s = Printf.printf "%d: %s\n" s.extra#id s.title;; > val print_metadata : < id : int; .. > t -> unit = > > Dario Teixeira writes: >> I have been trying to reach a sane modelling in OCaml for a "story" >> data structure in a CMS. The problem is that I find myself needing >> a degree of expressiveness that I can't find in the language! I do >> have a working, tentative solution, but it has a few ugly aspects >> that I would very much like to improve. Details follow. (Sorry >> for the long post; at least I hope it's not too dense and hard to >> follow). > > -- > Zheng Li > http://www.pps.jussieu.fr/~li > > _______________________________________________ > 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 > -- Zheng Li http://www.pps.jussieu.fr/~li