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.0 required=5.0 tests=none 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 31721BBC1 for ; Thu, 21 Feb 2008 18:03:28 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.25,387,1199660400"; d="scan'208";a="22881964" Received: from arvin.irisa.fr (HELO [131.254.11.86]) ([131.254.11.86]) by mail4-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 21 Feb 2008 18:03:27 +0100 Message-ID: <47BDADE9.4030902@free.fr> Date: Thu, 21 Feb 2008 17:59:21 +0100 From: "Tiphaine.Turpin" User-Agent: Thunderbird 1.5.0.10 (X11/20070303) MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Re: OO programming Content-Type: multipart/mixed; boundary="------------010909060408000905060205" X-Spam: no; 0.00; val:01 mutable:01 observers:01 observers:01 iter:01 val:01 mutable:01 iter:01 'self:01 ocaml:01 'self:01 restrictive:01 foo:01 obs:98 obs:98 X-Attachments: cset="UTF-8" name="Re: [Caml-list] Re: OO programming" name="Re: [Caml-list] Re: OO programming" This is a multi-part message in MIME format. --------------010909060408000905060205 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit --------------010909060408000905060205 Content-Type: message/rfc822; name="Re: [Caml-list] Re: OO programming" Content-Transfer-Encoding: 8bit Content-Disposition: inline; filename="Re: [Caml-list] Re: OO programming" Message-ID: <47BDAD15.9020501@free.fr> Date: Thu, 21 Feb 2008 17:55:49 +0100 From: "Tiphaine.Turpin" User-Agent: Thunderbird 1.5.0.10 (X11/20070303) MIME-Version: 1.0 To: Remi Vanicat Subject: Re: [Caml-list] Re: OO programming References: <47BD44FE.3050001@irisa.fr> <87myputonm.dlv@maison.homelinux.org> In-Reply-To: <87myputonm.dlv@maison.homelinux.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Remi Vanicat a écrit : > something like that might work (from the Dider Remy example) > > > class ['observer] subject = > object (self : 'mytype) > val mutable observers : 'observer list = [] > method add obs = observers <- obs :: observers > method notify (message : 'observer -> 'mytype -> unit) = > List.iter (fun obs -> message obs self) observers > end;; > > class ['subject] observer = > object > constraint 'subject = 'a #subject > end;; > > Note that it doesn't solve completely the problem (as #subject is > still an open type) but it might catch some problem. > > Thanks for your answer. It seems to be an interesting direction. Here is a try to expand the example further, where I assume that messages will go through one single method : class ['observer] subject = object (self : 'mytype) val mutable observers : 'observer list = [] method add obs = observers <- obs :: observers method notify (message : 'message) = List.iter (fun obs -> obs#send message self) observers end class virtual ['subject, 'message] observer = object (self : 'self) constraint 'subject = 'observer #subject method virtual send : 'message -> 'subject -> unit end This is still not enough, as I can for example, forget the 'subject argument in the type of send, without any type error (at this point). However, adding the constraint constraint 'observer = (_, _) #observer in the observer class does the work: if I write method virtual send : 'message -> 'subject -> unit then the class is rejected (with a horrible message, though, which I don't reproduce here to avoid hurting the sensibility of inocent ocaml users). the two classes seem to be "usable". Still, there is no link between the type 'self in the observer, and the type of the observer as viewed by the subject. I don't have a precise example in mind, but I feel that something is missing. A stronger possibility (which doesn't work) is to add the following constraint instead : constraint 'observer = 'self The class is rejected and I even get an understandable message: method virtual send : 'message -> unit ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The method send has type 'a -> unit but is expected to have type 'a -> < send : 'b; .. > #subject -> unit as 'b However, this constraint seems to be too restrictive, since, as I understand, it forces the subject to know the exact type of the observers which prevents (at least in my first tries) to add to a same subject different sub-classes of observer (or maybe I'm not using the right coercion). let s = new subject let o = object inherit [_, _] observer method send _ _ = () method foo = () end let _ = s#add (o :> (_, _) observer) => long complicated message Tiphaine Turpin --------------010909060408000905060205--