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=0.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE 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 6456ABC69 for ; Thu, 18 Oct 2007 17:42:08 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAABEcF0fZDAs+hWdsb2JhbACOTQIBCAQGDxMHgSc X-IronPort-AV: E=Sophos;i="4.21,296,1188770400"; d="scan'208";a="18225796" Received: from smtp008.mail.ukl.yahoo.com ([217.12.11.62]) by mail4-smtp-sop.national.inria.fr with SMTP; 18 Oct 2007 17:42:07 +0200 Received: (qmail 42077 invoked from network); 18 Oct 2007 15:42:06 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.fr; h=Received:X-YMail-OSG:In-Reply-To:References:Mime-Version:Content-Type:Message-Id:Cc:Content-Transfer-Encoding:From:Subject:Date:To:X-Mailer; b=fQfn0BMEyHxZQr+tUmM5Mn+TX1BsjS7R5bb1elVXL52kUBzVc34uGUoTYxfzXiAqOMT8B/SFqj+11p8qbwjGfmsJ1sVCmcFMrp76l/rBY/3nQrDUfyWOx0C2eZS2NPumy0JphPQB9Yf6cUh/tYHaosiqrKydSbYLMndILyFl5I4= ; Received: from unknown (HELO ?192.168.0.11?) (vincent.aravantinos@82.229.199.66 with plain) by smtp008.mail.ukl.yahoo.com with SMTP; 18 Oct 2007 15:42:06 -0000 X-YMail-OSG: MatKrh0VM1kVlp1UAk9va.ruINy8lWPDYmp3gi_ChL0LByg3TLrcS5y2XtdljmbKI5WJFKrDiQ-- In-Reply-To: <932096.75090.qm@web54602.mail.re2.yahoo.com> References: <932096.75090.qm@web54602.mail.re2.yahoo.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed Message-Id: <189AC25B-86C0-42ED-A2C4-38C7F1015840@yahoo.fr> Cc: caml-list@yquem.inria.fr Content-Transfer-Encoding: quoted-printable From: Vincent Aravantinos Subject: Re: [Caml-list] Smells like duck-typing Date: Thu, 18 Oct 2007 17:42:04 +0200 To: Dario Teixeira X-Mailer: Apple Mail (2.752.2) X-Spam: no; 0.00; ocaml:01 conceptually:01 variants:01 cheers:01 polymorphic:01 caml-list:01 functions:01 int:01 int:01 data:02 expression:02 expression:02 usable:02 string:02 string:02 Le 17 oct. 07 =E0 15:35, Dario Teixeira a =E9crit : > Hi, > > I have been trying to reach a sane modelling in OCaml for a "story" > data structure in a CMS. (...) > b) Use only full_t and make all fields option types. However, not =20 > only is > this cumbersome to use, but is also conceptually wrong, because =20 > it does > not capture the fact that, for example, all "blurb" stories have =20= > three > *mandatory* fields. The following uses polymorphic variants for options so as to make =20 those fields mandatory (thus it is more safe). However it remains very cumbersome :)) but I found it interesting =20 (and quite funny). And to follow the current topic, this solution makes inheritance the =20 right way (or, at least, the way you want). But honestly, I don't think it's usable (?) -------------- (* TYPES *) type ('a,'b) polyoption =3D [<`Some of 'a|`None] as 'b type 'a string_option =3D (string,[<`None|`Some of string] as 'a) =20 polyoption type 'a int_option =3D (int,[<`None|`Some of int] as 'a) polyoption type some_string =3D [`Some of string] type some_int =3D [`Some of int] type nothing =3D [`None] type ('a,'b,'c,'d) t =3D { id: 'a int_option; title: 'b string_option; intro: 'c string_option; body: 'd string_option } (* The types we will finally use *) type full_t =3D (some_int, some_string, some_string, some_string) t type blurp_t =3D (some_int, some_string, some_string, nothing ) t type fresh_t =3D (nothing , some_string, some_string, some_string) t (* SOME TESTS *) let full : full_t =3D {id =3D `Some 1; title =3D `Some "blablax"; intro = =3D =20 `Some "blox" ; body =3D `Some "pouetx"} (* Check with a missing field: # let badfull : full_t =3D {id =3D `None; title =3D `Some "blablax"; =20= intro =3D `Some "blox" ; body =3D `Some "pouetx"};; This expression has type (...) *) let blurp : blurp_t =3D {id =3D `Some 2; title =3D `Some "blablay"; = intro =3D =20 `Some "bloy" ; body =3D `None} (* Check with too much fields: # let badblurp : blurp_t =3D {id =3D `Some 2; title =3D `Some = "blablay"; =20 intro =3D `Some "bloy" ; body =3D `Some "pouety"};; This expression has type (...) *) let fresh : fresh_t =3D {id =3D `None; title =3D `Some "blablaz"; intro = =3D =20 `Some "bloz" ; body =3D `Some "pouetz"} (* FUNCTIONS *) let get_body : ('a,'b,'c,'d) t -> string =3D fun x -> let `Some x =3D =20= x.body in x let get_title : ('a,'b,'c,'d) t -> string =3D fun x -> let `Some x =3D =20= x.title in x;; ------------------ TESTS: # get_title full;; - : string =3D "blablax" # get_title blurp;; - : string =3D "blablay" # get_title fresh;; - : string =3D "blablaz" # get_body full;; - : string =3D "pouetx" # get_body blurp;; This expression has type (...) # get_body fresh;; - : string =3D "pouetz" Isn't it funny ? Cheers Vincent=