comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: How to get generic formal parameter type into base class
Date: Sat, 6 Oct 2012 20:06:34 -0700 (PDT)
Date: 2012-10-06T20:06:34-07:00	[thread overview]
Message-ID: <af37c0d9-81ca-42f4-8975-9245cc626d48@googlegroups.com> (raw)
In-Reply-To: <527f8487-a578-406c-86c3-b3b0596cda71@l32g2000yqb.googlegroups.com>

On Saturday, October 6, 2012 1:03:34 PM UTC-7, kevin andrew wrote:

> I still don't see how to get a dynamic type into base class so that I
> can dispatch to a function with that type in the function signature.
> If I do a Root_Type then I don't see how I can include the Get/
> Set_Value function because Data_Type is unknown? 

Right.  It was a bit late when I posted my response, so I didn't notice that Get/Set_Value would involve different result/parameter types so it couldn't be defined for the root type.  But if the procedure involves different result/parameter types, you can't really **dispatch** to it, can you?  For dispatching to work (and I think this applies to C++ and other OO languages as well), the types of everything except your Xlate_Type or Xlate_Base_Type have to be known; then the program decides at runtime which is the correct procedure or function to call.

In your case, I think what you want is this: Define Encode and Decode for your root type, but not Get_Value or Set_Value.  Go ahead and define Get_Value and Set_Value in the generic, but note that each instantiation of the generic will produce a different, and unrelated, declaration of Get_Value or Set_Value.  Since they're unrelated, you can't use a dispatching call to call them.  But your code wasn't trying to do that.  (In essence, you were using your own CASE statement to do a fake "dispatch".)  So when you wrote:

>       when Int_100 =>
>          Msg.theInt := Classes.Translate_Base.Get_Value(Class); --

if Class is declared with type Your_Root_Type'Class, you'll need to convert it to the specific type, and call the function declared in the specific generic instance:

         when Int_100 =>
           Msg.theInt := Objects.Int_100.Get_Value 
                           (Objects.Int_100.Xlate_Type(Class));

or, more simply:

         when Int_100 =>
           Msg.theInt := Objects.Int_100.Xlate_Type(Class).Get_Value;

Objects.Int_100.Xlate_Type(Class) is a kind of "type conversion" that tells the compiler that Class, which is declared as Your_Root_Type'Class, actually has the type Objects.Int_100.Xlate_Type (and it will check at run time that this is true, and raise an exception if not).

Please note, I'm at home and don't have easy access to an Ada compiler, so I can't try this myself to make sure it works.

Hope this helps,

                            -- Adam



  reply	other threads:[~2012-10-07  3:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-05 22:23 How to get generic formal parameter type into base class kevin.miscellaneous
2012-10-05 23:38 ` Georg Bauhaus
2012-10-06  3:28 ` Adam Beneschan
2012-10-06 20:03   ` kevin andrew
2012-10-07  3:06     ` Adam Beneschan [this message]
2012-10-11 20:25       ` kevin andrew
2012-10-11 23:28         ` Georg Bauhaus
2012-10-11 23:52         ` Georg Bauhaus
2012-10-07  7:42     ` Dmitry A. Kazakov
replies disabled

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