comp.lang.ada
 help / color / mirror / Atom feed
* Disallowing methods [use for multiple dispatch]
@ 2002-03-25 14:50 Dmitry A. Kazakov
  0 siblings, 0 replies; only message in thread
From: Dmitry A. Kazakov @ 2002-03-25 14:50 UTC (permalink / raw)


Hi all!

Methods of tagged types cannot be disallowed in Ada. [For other types
one can disallow predefined operations by making them abstract]. Here
is an example when disallowing seem to be necessary for tagged types.
Consider a dyadic primitive operation. It is a clear case for true
multiple dispatch. Unfortunately, Ada 95 has no proper one [tags must
be all same] . But there is a work-around. The following trick of
cascading dispatch can be used to emulate true MD:

package AA is
   type A is tagged null record;
--
-- Dyadic operation. Note the second arg is not dispatching to avoid
-- run-time errors.
--
   function "and" (X : A; Y : A'Class) return Boolean;
end AA;
-----------------------------------------
with Text_IO; use Text_IO;
package body AA is
   function "and" (X : A; Y : A'Class) return Boolean is
   begin
      if Y in A then
         Put_Line ("A and A"); -- Do it here
         return True;
      else
         return Y and X; -- Dispatch on the second argument
      end if;
   end "and";
end AA;
-----------------------------------------
with AA; use AA;
package BB is
   type B is new A with null record;
--
-- Overriding of the dyadic operation. BB is responsible to
-- care of all cases except A and A.
--
   function "and" (X : B; Y : A'Class) return Boolean;
end BB;
-----------------------------------------
with Text_IO; use Text_IO;
package body BB is
   function "and" (X : B; Y : A'Class) return Boolean is
   begin
      if Y in A then
         Put_Line ("B and A"); -- Do it here
         return True;
      elsif Y in B then
         Put_Line ("B and B"); -- Do it here
         return True;
      else
         return Y and X; -- Dispatch on the second argument 
      end if;
   end "and";
end BB;
-----------------------------------------
with AA; use AA;
with BB; use BB;
procedure Test is
   X : A;
   Y : B;
   Z : Boolean;
begin
   Z := X and X;
   Z := Y and X;
   Z := X and Y;
   Z := Y and Y;
end Test;
-----------------------------------------
The above works fine printing as expected:

A and A
B and A
B and A
B and B

But there is a great problem with the predefined equality operator
"=". Change "and" to "=" and the above will not work, because

   function "=" (X : A; Y : A'Class) return Boolean;

does not override the predefined

   function "=" (X, Y : A) return Boolean;

Therefore both get overloaded! This is a case when an ability to
disallow an operation would be very desirable. More generally it would
be nice to have an explicit hidding construct for hidding. Like:

   function "=" (X, Y : A) return Boolean is null;

The way limited vs. non-limited types is not enough flexible, IMO.

Regards,
Dmitry Kazakov



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-03-25 14:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-25 14:50 Disallowing methods [use for multiple dispatch] Dmitry A. Kazakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox