comp.lang.ada
 help / color / mirror / Atom feed
From: rick H <rik_nntp@dsl.pipex.com>
Subject: Re: Instantiating private types with discriminants?
Date: Wed, 10 May 2006 13:44:00 GMT
Date: 2006-05-10T13:44:00+00:00	[thread overview]
Message-ID: <A6m8g.29161$_k2.508345@news2.nokia.com> (raw)
In-Reply-To: 1147252198.138173.203910@j73g2000cwa.googlegroups.com

Ludovic Brenta <ludovic@ludovic-brenta.org> wrote:
> rick H writes:
>> Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote:
>> > BTW, You should probably make Put a primitive operation of General_T. Then
>> > the whole could look like simply:
>> >
>> >   Put (Var_A.all); -- Do what have to be done
>>
>> I'm still a bit new to Ada terminology - does "making Put a primitive
>> operation" mean doing this kind of thing with a generic package...
>>    type Fred is new Integer;
>>    package Fred_IO is new Ada.Text_IO.Integer_IO (Fred);
>>    use Fred_IO;
>> If that's right - how would I do this in my example? I'm puzzled
>> because Var_A.all.Data will resolve to either an Integer or a Float at
>> run-time.  Have I got to declare two new packages - one against
>> Integer_IO and one for Float_IO and let the dispatcher get it right
>> at run-time?
> 
> A "primitive operation" of a type T is a subprogram that:
> 
> - takes a parameter of type T or "access T", or returns a result of
> type T or "access T"
> 
> - is declared in the same package specification as T (same declarative
> region, really)
> 
> If these conditions are met, then the operation is primitive.  If, in
> addition, T is tagged, then a call to a primitive operation dispatches
> dynamically at run time according to the exact type T. Essentially, the
> compiler does the tag comparison and dispatching for you. So you could
> write:
> 
> package P is
>   type T is tagged null record;
>   procedure Put (Item : in T); -- primitive operation
> 
>   type Type_A is new T with Data : Integer; end record;
>   procedure Put (Item : in Type_A); -- primitive operation
> 
>   type Type_B is new T with Data : Float; end record;
>   procedure Put (Item : in Type_B); -- also primitive
> end P;
> 
> package body P is
>   procedure Put (Item : in T) is
>   begin
>       null;
>   end Put; -- we could also have declared this procedure abstract
> 
> procedure Put (Item : in Type_A) is
> begin
>   Ada.Integer_Text_IO.Put (Item.Data);
> end Put;
> 
> procedure Put (Item : in Type_B) is
> begin
>   Ada.Float_Text_IO.Put (Item.Data);
> end Put;
> end P;
> 
> with P;
> procedure Simple_Case is
>   Var_A : P.T'Class := ...; --  class-wide type: could be T, Type_A or
> Type_B
> begin
>   P.Put (Var_A); -- dynamic dispatch to suitable version of Put
> end Simple_Case;
> 

Thanks, Ludovic.  Bearing in mind that, in my original example, Var_A is
declared as an *access* to the class-wide type, I tried declaring the
various Put procedures as you showed, and then simply dereferencing
Var_A when calling Put:
   Put (Var_A.all);
However, the compiler (gnat) complained that I cannot use a class-wide
argument in this case.
What I had to do is to create two new types - one to access Type_A and
one to access Type_B.  Then I can call Put without dereferencing Var_A,
but at the expense of having to perform a type-conversion, e.g.
   Put (Type_B_Ptr(Var_A));

Obviously this gets me back to where I was before - having to check the
type prior to converting that type to either Type_A_Ptr or to
Type_B_Ptr...

if Var_A.all in Type_A then
   Put (Type_A_Ptr (Var_A));
elsif Var_A.all in Type_B then
   Put (Type_A_Ptr (Var_A));
end if;



Rick



  parent reply	other threads:[~2006-05-10 13:44 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-09 13:17 Instantiating private types with discriminants? rick H
2006-05-09 13:45 ` Georg Bauhaus
2006-05-09 14:06   ` rick H
2006-05-09 13:56 ` Ludovic Brenta
2006-05-09 14:24   ` rick H
2006-05-09 19:48     ` Ludovic Brenta
2006-05-09 14:05 ` Dmitry A. Kazakov
2006-05-09 14:48 ` rick H
2006-05-09 15:20   ` Jerry Petrey
2006-05-09 15:42     ` rick H
2006-05-09 15:53   ` Avoiding use Ada.Tags (was Re: Instantiating private types with discriminants?) Alex R. Mosteo
2006-05-09 16:01   ` Instantiating private types with discriminants? Dmitry A. Kazakov
2006-05-10  7:42     ` rick H
2006-05-10  9:09       ` Ludovic Brenta
2006-05-10 11:49         ` Georg Bauhaus
2006-05-10 13:44         ` rick H [this message]
2006-05-10 14:21           ` Ludovic Brenta
2006-05-10 15:10             ` rick H
2006-05-10 15:45               ` Ludovic Brenta
2006-05-10 14:41           ` Dmitry A. Kazakov
2006-05-10 15:34             ` rick H
2006-05-10 19:01               ` Georg Bauhaus
2006-05-10 19:05                 ` Ludovic Brenta
2006-05-10 21:52                   ` Rick H
2006-05-11  1:17                     ` Jeffrey R. Carter
2006-05-11  7:44                     ` Dmitry A. Kazakov
2006-05-11  8:27                       ` rick H
2006-05-11 10:28                         ` Dmitry A. Kazakov
2006-05-11 15:59                           ` Robert A Duff
2006-05-12  7:37                             ` Dmitry A. Kazakov
2006-05-12  9:24                               ` Georg Bauhaus
2006-05-12 12:40                                 ` Dmitry A. Kazakov
2006-05-12 18:25                                   ` Randy Brukardt
2006-05-09 19:57   ` "Use" and "=" for Tags (was: Re: Instantiating private types with discriminants?) Jeffrey R. Carter
replies disabled

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