From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,dad94612ff745427 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!newsfeed.gamma.ru!Gamma.RU!npeer.de.kpn-eurorings.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Instantiating private types with discriminants? Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Tue, 9 May 2006 16:05:37 +0200 Message-ID: <1245dxetj2gue$.rywnm0yiwsg8$.dlg@40tude.net> NNTP-Posting-Date: 09 May 2006 16:05:37 MEST NNTP-Posting-Host: 8a386113.newsread4.arcor-online.net X-Trace: DXC=k;=d]iDN0iNo47S\@TX3oE:ejgIfPPldDjW\KbG]kaMH]kI_X=5KeaFHoCoXf1XQIK[6LHn;2LCVN7enW;^6ZC`DIXm65S@:3>O X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:4151 Date: 2006-05-09T16:05:37+02:00 List-Id: 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