From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Instantiating private types with discriminants?
Date: Tue, 9 May 2006 16:05:37 +0200
Date: 2006-05-09T16:05:37+02:00 [thread overview]
Message-ID: <1245dxetj2gue$.rywnm0yiwsg8$.dlg@40tude.net> (raw)
In-Reply-To: QD08g.28768$Nb2.521930@news1.nokia.com
On Tue, 09 May 2006 13:17:36 GMT, rick H wrote:
> I'm slowly learning Ada for my own amusement, and I've ground to a halt
> trying to understand something. If some kind sole could explain it to
> me, I'd be very grateful.
>
> I've defined two types, each with a descriminant, and each with its own
> access type. One of the type's implementations is, however, private:
>
> package Discrim is
> type Type_A (Param : Integer := 100) is null record;
> type Type_A_Ptr is access Type_A;
>
> -- same as above, but implementation now private...
> type Type_B (Param : Integer := 100) is private;
> type Type_B_Ptr is access Type_B;
> private
> type Type_B (Param : Integer := 100) is null record;
> end Discrim;
>
> When I use "new" on two variables declared as Type_A_Ptr and Type_B_Ptr,
> one requires a type conversion for the discriminant, whereas the other
> requires a qualified expression:
>
> with Discrim; use Discrim;
> procedure Use_Discrim is
> A : Type_A_Ptr; -- public implementation
> B : Type_B_Ptr; -- private implementation
> begin
> A := new Type_A'(Param => 100); -- qualified expression
> B := new Type_B (Param => 123); -- type conversion
> end Use_Discrim;
>
> So, my question to the experts is: Why does "privatising" a type's
> details change the way that you "new" instantiations of it?
They don't:
A := new Type_A (Param => 100); -- That's OK
The only difference is that you can't use aggregates with Type_B because
they are private. You don't know members. Note that you can still use
qualified expressions with Type_B if you have some other way of
constructing Type_B, without aggregates. So if Discrim had a function:
function Get_Me_Some_B return Type_B;
then:
B := new Type_B'(Get_Me_Some_B); -- qualified expression
The only purpose of a qualified expression is to specify the expected type.
Further
B := new Type_B (Param => 123);
is not a type conversion, it is constraining of Type_B. Type_B (Param =>
123) is a subtype specification. It is same as in:
B_Object : Type_B (Param => 123); -- subtype of Type_B
You can also compare:
A := new Type_A'(Param => 100);
with
A_Object : Type_A := (Param => 100);
Type_A is the type of A_Object, (Param => 100) is an expression resulting
in the initial value. It could also well be:
A_Object : Type_A (Param => 100) := (Param => 100);
Here Type_A ((Param => 100) is the subtype of Type_A for all values of
A_Object. (Param => 100) appearing after ":=" is an expression in the form
of an aggregate. You can qualify it if you want:
A_Object : Type_A (Param => 100) := Type_A'(Param => 100);
You can do all this with Type_B, except for record aggregates, because they
are private.
Note that both:
A_Object : Type_A;
A := new Type_A;
are OK, because you have provided a default value for the discriminant.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
next prev parent reply other threads:[~2006-05-09 14:05 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 [this message]
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
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