|
|||
|
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 ------------------------ |
|
|
||||
|
||||
|
|
|
|||
|
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> |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |