caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Restricting Method Overriding/Redefinition in Subclass
@ 2004-08-14  0:03 chris.danx
  2004-08-14  7:38 ` skaller
  0 siblings, 1 reply; 8+ messages in thread
From: chris.danx @ 2004-08-14  0:03 UTC (permalink / raw)
  To: Caml Mailing List

Hi,

Methods provide access control in the form of private or public methods, 
but I am in a position where I would like the client to extend some 
methods but not others, and would like some advice if possible.

class type virtual base_class =
    object (self)

    method add_child    (c : base_class) -> unit
    method remove_child (c : base_class) -> unit
    method remove_all_child (c : base_class) -> unit

    method private children : unit -> base_class iterator


    method virtual perform_action unit -> unit
end;;

perform_action is supposed to do something with the rendering state and 
call each of it's childrens perform_action methods in turn (unless it's 
a leaf).  base_class is the base class for all objects in a scene graph.

I have narrowed the behaviour of perform_action down to three 
possibilities.  Either it does something before calling it's children, 
it does something after calling it's children or it does something 
before and after calling it's children.  This leads either to one class 
with additional two methods (recode base_class to include pre and post 
ops) or three classes with either a pre or post call or both.  To ensure 
objects don't do extra work I was considering the three class solution 
or a set of classes paramterised by closures which resolve to 
essentially the same thing. e.g.

class type prechild_class =
    object(self)
       inherit base_class

    method virtual pre_op unit -> unit
    method perform_action unit -> unit
end;;

or

(* pre_op_func is a typically a closure and essentially behaves
    like a hook.
  *)
class type prechild_class (pre_op_func : unit -> unit) =
    object(self)
       inherit base_class

    ...

    method perform_action unit -> unit
end;;

Basically pre_op can be overridden to perform the desired function, with 
perform_action calling it before iterating over the children or a 
closure can be provided and called by perform_action.  Is it possible to 
prevent the redefinition of perform_action by a subclass?  If not I can 
live with it, but if there is a way it would serve to enforce the 
intended behaviour of the class.  Namely that it should do what it needs 
to do and call it's children somewhere in perform_action.  One could 
rely on the subclass to encode this behaviour, but it would be very easy 
for programmers to forget this which leads to a violation of what could 
be regarded as a class invariant.

Perhaps a radically different solution is best?  I chose an OO solution 
so that new classes could be trivially added to the system.  It just 
occured to me that the same functionality can be provided with records 
where all the operations are closures.  Pass in the relevant functions 
to a function and get a closure with all the operations of the original 
class.  It's entirely possible I'm over analysing the problem and should 
use something simpler as I've done that before on more than one occassion.

Any advice is welcome.


Cheers,
Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [Caml-list] Restricting Method Overriding/Redefinition in Subclass
@ 2004-08-16  4:58 Jeff Schultz
  0 siblings, 0 replies; 8+ messages in thread
From: Jeff Schultz @ 2004-08-16  4:58 UTC (permalink / raw)
  To: Caml Mailing List; +Cc: John Prevost

> 1) Implementing multiple interfaces.
> 
> In Java, things sometimes get awkward if you want to work with values
> that implement multiple interfaces at once:
> 
> class MyThingy implements Colored, HasPoint {
>     ...
> }
> 
> Okay, so the difficulty in this set of classes and interfaces (which
> *has* actually bitten me in the wild fairly frequently) is that there
> is no way to declare a variable as "any object that is of both type
> Colored and type HasPoint."  If we do this:
> 
> interface ColoredHasPoint extends Colored, HasPoint { ... }
> 
> then MyThingy is not an instance of ColoredHasPoint.  It has all of
> the appropriate methods, but it was not *declared* to have this type. 
> Therefore, it does not have the correct type.  So you end up having to
> write:
> 
>     void useColoredHasPoint(Object o) {
>         Colored c = (Colored) o;
>         HasPoint h = (HasPoint) o;
>         ... = c.getColor();
>         ... = h.getPoint();
>     }
> 
> and obviously the system has now broken down.  AS long as you work
> with only one interface at a time, you're in fine shape.  As soon as
> you need more than one and have no other requirements, you're in
> trouble.  In O'Caml, of course, the above is trivial:
> 
>     let use_colored_has_point o =
>        let color = o#get_color () in
>        let point = o#get_point () in
>        ...

True enough, but I think of this as more to do with the convenience
provided by type inference's creation of names for types the
programmer didn't write.  So the type of use_colored_has_point is
something like

    < get_color : unit -> 'a; get_point : unit -> 'b; .. > -> ...

where type inference has come up with a new type that the programmer
could have written, but didn't have to.

One can get the same effect with Java 1.5's poor man's generics by
writing

    <T extends Colored & HasPoint> void useColoredHasPoint2(T o) {
        Color color = o.getColor();
	Point point = o.getPoint();
    }

so even though it's verbose, it's certainly possible.  (If it also had
typedef, it might not be all that bad.)

Of course, just because an object has both get_color and get_point, or
implements both Colored and HasPoint interfaces, doesn't mean that the
colour and point returned have any specific relationship to each
other, so it's not a given that mixing them on any arbitrary object
makes sense.  To be safe, one needs some way to check that they do go
together, perhaps a ColoredPoint interface or the discipline of saying
that this getPoint method is here because this object implements
HasPoint, and obeys HasPoint's contract with the programmer, whatever
that is, isn't always such a bad thing :-)


    Jeff Schultz

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2004-08-18 22:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-14  0:03 [Caml-list] Restricting Method Overriding/Redefinition in Subclass chris.danx
2004-08-14  7:38 ` skaller
2004-08-14 16:28   ` John Prevost
2004-08-14 17:17     ` skaller
2004-08-18 12:04     ` chris.danx
2004-08-18 19:47       ` John Prevost
2004-08-18 22:21       ` skaller
2004-08-16  4:58 Jeff Schultz

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).