From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Dispatching to a common most special ancestor
Date: Thu, 12 Jun 2003 08:08:02 +0200
Date: 2003-06-12T08:08:02+02:00 [thread overview]
Message-ID: <bc956v$g484q$1@ID-77047.news.dfncis.de> (raw)
In-Reply-To: bc8cnl$in0$1@news.cs.tu-berlin.de
Stephan Heinemann wrote:
> Please have a look at the following code:
[...]
> I wanted Equals to be dispatched to the common object ancestor but
> instead a constraint error is raised. How might I resolve this?
There is a work-around for multiple dispatch. You might look at
http://www.dmitry-kazakov.de/ada/components.htm which presumably does what
you want. In short, you declare:
function Equal
( Left : Object;
Right : Object'Class;
Flag : Boolean := False
) return Boolean;
function Less
( Left : Object;
Right : Object'Class;
Flag : Boolean := False
) return Boolean;
and then override, for instance Less:
function Less
( Left : A_New_Object;
Right : Object'Class;
Flag : Boolean := False
) return Boolean is
begin
if ( Flag
or else
Right not in A_New_Object'Class
or else
Right in A_New_Object
)
then
-- Implement it here
...
else
-- Dispatch on the second parameter
return
not ( Less (Right, Left, True)
or else
Equal (Right, Left, True)
);
end if;
end Less;
The idea is that you do something if Left :> Right, otherwise you redispatch
through Right. For a commutative operation it is pretty straightforward.
For a non-commutative (like Less) it is a little bit tricky, as the code
shows.
Beware, that the predefined "=" cannot be disallowed, so you cannot do
function "=" (Left : Object; Right : Object'Class) is
begin
return Equal (Left, Right);
end "=";
because it would just overload the predefined one.
--
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de
next prev parent reply other threads:[~2003-06-12 6:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-06-11 23:07 Dispatching to a common most special ancestor Stephan Heinemann
2003-06-12 6:08 ` Dmitry A. Kazakov [this message]
2003-06-12 11:29 ` Georg Bauhaus
2003-06-12 15:03 ` Matthew Heaney
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox