comp.lang.ada
 help / color / mirror / Atom feed
* Base'Class is instance of Der1?
@ 2005-01-08 12:01 R
  2005-01-08 12:38 ` Martin Krischik
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: R @ 2005-01-08 12:01 UTC (permalink / raw)


Hello.

I've got function which has a parameter of type Base'Class.

well I have 3 derived tagged records and depending of what exactly is
the parameter I have to cast it to a proper function.

How can I figure out whether or not given object is der1 or der2 or
der3 type?

thanks in advance

best regards
R




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

* Re: Base'Class is instance of Der1?
  2005-01-08 12:01 Base'Class is instance of Der1? R
@ 2005-01-08 12:38 ` Martin Krischik
  2005-01-08 14:25   ` Martin Dowie
  2005-01-08 12:51 ` Ludovic Brenta
  2005-01-08 14:20 ` Dreni
  2 siblings, 1 reply; 7+ messages in thread
From: Martin Krischik @ 2005-01-08 12:38 UTC (permalink / raw)


Hi R

R wrote:

> Hello.
> 
> I've got function which has a parameter of type Base'Class.
> 
> well I have 3 derived tagged records and depending of what exactly is
> the parameter I have to cast it to a proper function.

Maybe you want a view rename instead of a cast:

http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Rename_View

> How can I figure out whether or not given object is der1 or der2 or
> der3 type?

You should rename to Base'Class.

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



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

* Re: Base'Class is instance of Der1?
  2005-01-08 12:01 Base'Class is instance of Der1? R
  2005-01-08 12:38 ` Martin Krischik
@ 2005-01-08 12:51 ` Ludovic Brenta
  2005-01-08 14:20 ` Dreni
  2 siblings, 0 replies; 7+ messages in thread
From: Ludovic Brenta @ 2005-01-08 12:51 UTC (permalink / raw)


"R" writes:
> Hello.
>
> I've got function which has a parameter of type Base'Class.
>
> well I have 3 derived tagged records and depending of what exactly is
> the parameter I have to cast it to a proper function.
>
> How can I figure out whether or not given object is der1 or der2 or
> der3 type?
>
> thanks in advance
>
> best regards
> R

This is called "dispatching", and Ada 95 supports it directly in the
language.  There is no need for you to explicitly check the actual
type:

package P is
   type Base is tagged abstract null record;
   procedure Proc (B : in Base) is abstract;

   type Der1 is new Base with null record;
   procedure Proc (D : in Der1);

   type Der2 is new Base with null record;
   procedure Proc (D : in Der2);

   type Der3 is new Base with null record;
   procedure Proc (D : in Der3);
end P;

with P;
procedure Dispatch (B : in P.Base'Class) is
begin
   P.Proc (B); -- dispatches to the appropriate Proc
end Dispatch;

To make this work, you must make sure that each instance of Proc is a
"primitive operation" of the appropriate type.  A "primitive
operation" is:

- declared in the same package as the type
- has at least one parameter or return value of the type

Primitive operations are defined in detail in ARM 3.2.3.

And you have to make sure that the point where you call P.Proc is a
"dispatching call".  For the call to be dispatching, the actual
parameter must be of a class-wide type, here Base'Class.

The precise rules for dispatching calls are in ARM 3.9.2.

Now, if you really insist on checking the actual type yourself:

with P;
procedure Dispatch (B : in P.Base'Class) is
begin
   if B in P.Der1'Class then
      -- dispatch
   elsif B in P.Der2'Class then
      -- dispatch
   ...
end Dispatch;

This is much more error-prone and less maintainable than
language-assisted dispatching.  I do not recommend this way.

-- 
Ludovic Brenta.



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

* Re: Base'Class is instance of Der1?
  2005-01-08 12:01 Base'Class is instance of Der1? R
  2005-01-08 12:38 ` Martin Krischik
  2005-01-08 12:51 ` Ludovic Brenta
@ 2005-01-08 14:20 ` Dreni
  2 siblings, 0 replies; 7+ messages in thread
From: Dreni @ 2005-01-08 14:20 UTC (permalink / raw)


tung




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

* Re: Base'Class is instance of Der1?
  2005-01-08 12:38 ` Martin Krischik
@ 2005-01-08 14:25   ` Martin Dowie
  2005-01-08 15:00     ` Martin Krischik
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Dowie @ 2005-01-08 14:25 UTC (permalink / raw)


Martin Krischik wrote:
> Hi R
> 
> R wrote:
> 
> 
>>Hello.
>>
>>I've got function which has a parameter of type Base'Class.
>>
>>well I have 3 derived tagged records and depending of what exactly is
>>the parameter I have to cast it to a proper function.
> 
> 
> Maybe you want a view rename instead of a cast:
> 
> http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Rename_View

Surely:

type Parent_Type is tagged null record;
type Child_Type is tagged null record;

Child_Instance : Child_Type;
Parent_View : Parent_Type'Class renames Parent_Type'Class (Child_Type);

Should be:

type Parent_Type is tagged null record;
type Child_Type is new Parent_Type with null record;

Child_Instance : Child_Type;
Parent_View : Parent_Type'Class renames Parent_Type'Class (Child_Instance);


Cheers

-- Martin



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

* Re: Base'Class is instance of Der1?
  2005-01-08 14:25   ` Martin Dowie
@ 2005-01-08 15:00     ` Martin Krischik
  2005-01-10  9:29       ` Martin Dowie
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Krischik @ 2005-01-08 15:00 UTC (permalink / raw)


Martin Dowie wrote:

> Martin Krischik wrote:
>> Hi R
>> 
>> R wrote:
>> 
>> 
>>>Hello.
>>>
>>>I've got function which has a parameter of type Base'Class.
>>>
>>>well I have 3 derived tagged records and depending of what exactly is
>>>the parameter I have to cast it to a proper function.
>> 
>> 
>> Maybe you want a view rename instead of a cast:
>> 
>> http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Rename_View
> 
> Surely:
> 
> type Parent_Type is tagged null record;
> type Child_Type is tagged null record;
> 
> Child_Instance : Child_Type;
> Parent_View : Parent_Type'Class renames Parent_Type'Class (Child_Type);
> 
> Should be:
> 
> type Parent_Type is tagged null record;
> type Child_Type is new Parent_Type with null record;
> 
> Child_Instance : Child_Type;
> Parent_View : Parent_Type'Class renames Parent_Type'Class
> (Child_Instance);

Ups, you are right! But why did you not hit the [edit] button and corrected
it?

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



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

* Re: Base'Class is instance of Der1?
  2005-01-08 15:00     ` Martin Krischik
@ 2005-01-10  9:29       ` Martin Dowie
  0 siblings, 0 replies; 7+ messages in thread
From: Martin Dowie @ 2005-01-10  9:29 UTC (permalink / raw)


Martin Krischik wrote:
> Ups, you are right! But why did you not hit the [edit] button and
> corrected it?

I'm too shy! ;-)

I did correct a few English-isms though...

Cheers

-- Martin






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

end of thread, other threads:[~2005-01-10  9:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-08 12:01 Base'Class is instance of Der1? R
2005-01-08 12:38 ` Martin Krischik
2005-01-08 14:25   ` Martin Dowie
2005-01-08 15:00     ` Martin Krischik
2005-01-10  9:29       ` Martin Dowie
2005-01-08 12:51 ` Ludovic Brenta
2005-01-08 14:20 ` Dreni

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