caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Tiphaine.Turpin" <Tiphaine.Turpin@free.fr>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Re: OO programming
Date: Thu, 21 Feb 2008 20:47:17 +0100	[thread overview]
Message-ID: <47BDD545.7020404@free.fr> (raw)
In-Reply-To: <47BDADE9.4030902@free.fr>

Tiphaine.Turpin a écrit :
>
>
> ------------------------------------------------------------------------
>
> Sujet:
> Re: [Caml-list] Re: OO programming
> Expéditeur:
> "Tiphaine.Turpin" <Tiphaine.Turpin@free.fr>
> Date:
> Thu, 21 Feb 2008 17:55:49 +0100
> Destinataire:
> Remi Vanicat <vanicat@debian.org>
>
> Destinataire:
> Remi Vanicat <vanicat@debian.org>
>
>
> 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.
Of course something is missing : if I just forget the method send, then 
I have no error, which is problematic.

>
>
> A stronger possibility (which doesn't work) is to add the following 
> constraint instead :
>
>  constraint 'observer = 'self
>
> The class is rejected
(when doing the same mistake as above: forgetting the 'subject argument, 
otherwise it typechecks normally)
> 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
>
In addition, if I now forget the virtual method send, it gets added 
automagically in the type !
> 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
>
I have the impression that the right constraint would be something like
" 'self must be coercible to 'observer "
but I don't know if this makes sense...
>
> Tiphaine Turpin
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> 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
>   


  reply	other threads:[~2008-02-21 19:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-21 16:59 Tiphaine.Turpin
2008-02-21 19:47 ` Tiphaine.Turpin [this message]
2008-02-21 23:56   ` Julien Moutinho
2008-02-22 13:52     ` Tiphaine.Turpin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47BDD545.7020404@free.fr \
    --to=tiphaine.turpin@free.fr \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).