Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 2 > Newsgroup comp.lang.ml

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-19-2004, 11:08 PM
toolslive
Guest
 
Posts: n/a
Default Problems with Observer pattern (downcasts)

Yo,

I'm trying to implement a variant of the observer pattern, ran into
problems because OCaml doesn't allow downcasts. I have most of it
running using parametrized classes but encountered following problem:
all of my observers need to have the same type signature.

The problem is illustrated in the code below (added end of the mail)
where
adding a method to the watcher2 class makes the type-system complain:

This expression has type
(< dump : string; .. > as 'a) watcher2 =
< dump : string; some_method : unit -> unit; update : 'a -> unit >
but is here used with type
event watcher = < dump : string; update : event -> unit >
Only the first object type has a method some_method

in the call s#add_observer w2.

The question is:
how do I get around this problem ?

tia,
Tools

------------------------------observer example ----------------------
class event = object(self)
method dump = "event!"
end

class virtual ['event] observer =
object(self)
method virtual update: 'event -> unit
method dump = "observer"
end


class ['observer] observable =
object(self)
val mutable _observers = ([]:'observer list)

method add_observer obs= _observers <- obs::_observers

method _notify ev= List.iter (fun x -> x#update ev) _observers
end


class ['event] watcher = object(self)
inherit ['event] observer
method update event =
Printf.printf "watcher got %s\n" event#dump;
()
end

class ['observer] seen = object(self)
inherit ['observer] observable
method do_it =
let event = new event in
self#_notify event

end

class ['event] watcher2 = object(self)
inherit ['event] watcher

method update event =
Printf.printf "watcher2 got %s\n" event#dump

(* adding this method makes the type system complain
method some_method () = ()
*)
end

let go ()=
let w= new watcher in
let s= new seen in
let w2 = new watcher2 in
s#add_observer w;
s#add_observer w2;
s#do_it;;

go();;
---------------------- end of example ------------------------

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-20-2004, 03:09 AM
Jacques Garrigue
Guest
 
Posts: n/a
Default Re: Problems with Observer pattern (downcasts)

yahoo.com">toolslive@yahoo.com (toolslive) writes:

> I'm trying to implement a variant of the observer pattern, ran into
> problems because OCaml doesn't allow downcasts. I have most of it
> running using parametrized classes but encountered following problem:
> all of my observers need to have the same type signature.
>
> The problem is illustrated in the code below (added end of the mail)
> where
> adding a method to the watcher2 class makes the type-system complain:
>
> This expression has type
> (< dump : string; .. > as 'a) watcher2 =
> < dump : string; some_method : unit -> unit; update : 'a -> unit >
> but is here used with type
> event watcher = < dump : string; update : event -> unit >
> Only the first object type has a method some_method
>
> in the call s#add_observer w2.
>
> The question is:
> how do I get around this problem ?


You just need to cast all your observers to the same type.
I.e., this is not a downcast problem (not allowed in ocaml),
but an upcast problem (allowed, but has to be explicit).

Replace the incriminated line by

s#add_observer (w2 :> _ watcher);

> ------------------------------observer example ----------------------
> class event = object(self)
> method dump = "event!"
> end
>
> class virtual ['event] observer =
> object(self)
> method virtual update: 'event -> unit
> method dump = "observer"
> end
>
>
> class ['observer] observable =
> object(self)
> val mutable _observers = ([]:'observer list)
>
> method add_observer obs= _observers <- obs::_observers
>
> method _notify ev= List.iter (fun x -> x#update ev) _observers
> end
>
>
> class ['event] watcher = object(self)
> inherit ['event] observer
> method update event =
> Printf.printf "watcher got %s\n" event#dump;
> ()
> end
>
> class ['observer] seen = object(self)
> inherit ['observer] observable
> method do_it =
> let event = new event in
> self#_notify event
>
> end
>
> class ['event] watcher2 = object(self)
> inherit ['event] watcher
>
> method update event =
> Printf.printf "watcher2 got %s\n" event#dump
>
> (* adding this method makes the type system complain
> method some_method () = ()
> *)
> end
>
> let go ()=
> let w= new watcher in
> let s= new seen in
> let w2 = new watcher2 in
> s#add_observer w;
> s#add_observer w2;
> s#do_it;;
>
> go();;
> ---------------------- end of example ------------------------


---------------------------------------------------------------------------
Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp
<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Problems loading character variables into Oracle Gerry Newsgroup comp.soft-sys.sas 0 11-15-2006 08:29 PM
Re: SAS Tip: Using pattern matching in the Where statement. David L. Cassell Newsgroup comp.soft-sys.sas 0 05-02-2005 09:09 PM
SAS Tip: Using pattern matching in the Where statement. Wright, Wendi Newsgroup comp.soft-sys.sas 0 05-02-2005 02:24 PM
Re: Counting number of occurences of a pattern in a string Dunn, Toby Newsgroup comp.soft-sys.sas 2 11-22-2004 10:33 PM
Counting number of occurences of a pattern in a string George Lesny Newsgroup comp.soft-sys.sas 1 11-15-2004 01:55 PM



All times are GMT. The time now is 06:47 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.