Re: Optimization for OOP



On Mon, 5 May 2008 18:51:58 -0700 (PDT), lucretia9@xxxxxxxxxxx wrote:

On 5 May, 17:37, "Dmitry A. Kazakov" <mail...@xxxxxxxxxxxxxxxxx>
wrote:

I think a consistent types system would be a better way. In Ada it
is always known if a call is dispatching or not. That is because of
proper typing. When an object is of a specific type, its methods
never dispatch, for that obvious reason, that the type is known to
be specific.

From what I've read about it, in Ada you can actually statically or
dynamically dispatch depending on *how* you call the subprogram. I've
not tried this BTW, but would love to see an example :D

Strictly speaking it does not depend on "how", it depends on "what", i.e.
on the object's type:

type T is tagged null record; -- a type
procedure Foo (X : T); -- a method of T

type S is new T with null record; -- a derived type
procedure Foo (X : S); -- Foo gets overridden for S

O1 : T; -- the type is specific T
O2 : S; -- the type is specific S
O3 : T'Class := O1; -- the type is unspecific, any derived from T
O4 : T'Class := O2; -- like above

Foo (O1); -- statically resolved to Foo of T
Foo (O2); -- statically resolved to Foo of S
Foo (O3); -- dispatches to Foo of T
Foo (O4); -- dispatches to Foo of S

O3 and O4 are polymorphic objects (Ada uses the term "class-wide"). IFF a
method (Ada calls it "primitive operation") of some type T is called on a
polymorphic object from the class of T, that dispatches according to the
type tag of the object.

The types of polymorphic and specific objects are different in Ada. They
are T'Class and T respectively. This eliminates re-dispatch in methods and,
though Ada does not go that far, potentially allows an implementation of
multiple dispatch, as well as classes of small scalar types with value
semantics, like Boolean and Integer (without space/performance overhead).

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.



Relevant Pages

  • Re: Is Double Dispatch really object-oriented?
    ... Compile error, in a language that supports MD properly => ... No dispatch ever fails ... Do you seriously claim that you can test Foo having just one B? ...
    (comp.object)
  • Re: tagged record child: override constructor?
    ... It is error-prone because of enforced re-dispatch One ... should explicitly specify T::Fooreferring member functions within a ... procedure Foo is ... -- This does not dispatch! ...
    (comp.lang.ada)
  • Re: Is Double Dispatch really object-oriented?
    ... No dispatch ever fails ... Also if polymorphism is an essential part of OO, ... Do you seriously claim that you can test Foo having just one B? ... The contract of A is the only way. ...
    (comp.object)
  • Re: Disadvantages of Inheritance, Polymorphism and Encapsulation
    ... Assuming that the Xeons can manage to issue about 2 instructions per ... They take _HALF_ the cost of the method dispatch. ... Lets look at some code snippets [BTW: I'm compiling foo and bar with -O4 ...
    (comp.object)
  • Re: abstract sub programs overriding
    ... To dispatch vs. not has to be defined by solely the object type. ... is the only right way, IMO, and Ada conforms to it. ... but Initialize is not a constructor in C++ sense. ...
    (comp.lang.ada)