Re: Events, method iterator

From: Markus Mottl (mottl@miss.wu-wien.ac.at)
Date: Mon May 31 1999 - 22:17:46 MET DST


From: Markus Mottl <mottl@miss.wu-wien.ac.at>
Message-Id: <199905311917.VAA04030@miss.wu-wien.ac.at>
Subject: Re: Events, method iterator
To: u009731@alpcom.it (Ubik)
Date: Mon, 31 May 1999 21:17:46 +0100 (MET DST)
In-Reply-To: <4.1.19990531191134.00939130@email.alpcom.it> from "Ubik" at May 31, 99 07:20:10 pm

> >> There's no way to define an
> >> explicit function to send messages to objects ?
> >
> >Yes, there is: just use unbound (=lambda-) functions. E.g.:
> >
> > List.iter (fun obj -> obj#paint) _objs
>
> Yeah, this is a step in the direction I intended, thanks, but I think i
> can't write a sort of
>
> let send msg obj = obj#msg (* I know this won't work *)

Yes, this won't work, because OCAML relies on static type checking only
(I think this is good). It is impossible to decide at compile time
whether all objects "obj" passed to "send" really implement message "msg".

> to be used like this :
> ...
> List.iter (send paint) _objs
>
> ...
> List.iter (send (moveRel 20 20)) _objs

Two possibilities:

You can define an algebraic data type of messages. E.g.

  type action = Paint
              | MoveRel of int * int

  let send action obj = match action with
    | Paint -> obj#paint
    | MoveRel (x,y) -> obj#move_rel x y

  class graph_obj name = object
    method paint = Printf.printf "%s#paint\n" name
    method move_rel x y = Printf.printf "%s#move_rel %d %d\n" name x y
  end

  let _ =
    List.iter (send (MoveRel(20,20)))
      [new graph_obj "first"; new graph_obj "second"]

But I prefer the version, where you have to define the functions (shorter
and clearer):

  let paint obj = obj#paint
  let move_rel x y obj = obj#move_rel x y

  class graph_obj name = object
    method paint = Printf.printf "%s#paint\n" name
    method move_rel x y = Printf.printf "%s#move_rel %d %d\n" name x y
  end

  let _ =
    List.iter (move_rel 20 20) [new graph_obj "first"; new graph_obj "second"]

This is hardly any effort and works just fine.

Don't forget that functions are first class values in OCAML!! You can
pass them around easily (including functions that encapsulate method
calls)! E.g.:

  let paint obj = obj#paint
  let move_rel x y obj = obj#move_rel x y

  class graph_obj name = object
    val mutable x = 0
    val mutable y = 0
    method paint = Printf.printf "%s#paint\n" name
    method move_rel dx dy =
      x <- x + dx; y <- y + dy;
      Printf.printf "%s#move_rel %d %d -> x:%d y:%d\n" name dx dy x y
  end

  let _ =
    let obj_lst = [new graph_obj "first"; new graph_obj "second"]
    and msg_lst = [move_rel 42 10; move_rel (-5) 7; paint] in
    List.iter (fun msg -> List.iter msg obj_lst) msg_lst

Syntactically convenient enough? :-)

Well, I could imagine a slightly more convenient version if there were
some kind of operator which automatically creates a function that applies
a method to an object. Maybe something like:

  List.iter (#move_rel 10 20) obj_lst

Another use:

  let msg_lst = [#move_rel 42 10; #move_rel (-5) 7; #paint]

Thus, the encapsulating function does not have to be necessarily bound
(keeps the namespace tidy) and the programmer can see from the syntax
that it is methods that get invoked.

I don't know whether the implementors want to add such a kind of syntactic
sugar... any suggestions?

> I know the result is the same, but I prefer a solution like this,
> I like sintactic-clean code ...

Me, too. But I like semantically correct code even more - and thus I
don't want to have my programs crash, just because a message is passed
to an object, which can't handle it. But as you can see, the present
concept is just fine...

Best regards,
Markus

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:22 MET