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: a07f3367d7,ba1efd580c7c1290 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!feed.xsnews.nl!border-4.ams.xsnews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Dynamic allocation of unconstrained types 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: Wed, 30 Sep 2009 16:56:41 +0200 Message-ID: <1qy4b03u1mtz1.1ri6symgrtrgo$.dlg@40tude.net> NNTP-Posting-Date: 30 Sep 2009 16:56:42 CEST NNTP-Posting-Host: 9ac11d98.newsspool3.arcor-online.net X-Trace: DXC=oJMb>Pb7H;T5TOT9_N5inj\>j@YDNcfSJ;bb[UFCTGGVUmh?TLK[5LiR>kgRiVDo]b0eSSQ X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:8541 Date: 2009-09-30T16:56:42+02:00 List-Id: On Wed, 30 Sep 2009 07:29:26 -0700 (PDT), Maciej Sobczak wrote: > Consider: > > procedure Test is > > package P is > type T (<>) is limited private; > function Create return T; > private > type T is limited record > I : Integer; > end record; > end P; > > package body P is > function Create return T is > begin > return T'(I => 123); > end Create; > end P; > > S : access P.T; > > begin > S := new P.T'(P.Create); -- ??? (this is line 22) > end Test; > > GNAT says: > > test.adb:22:19: uninitialized unconstrained allocation not allowed > test.adb:22:19: qualified expression required > > Interestingly, it works with Strings. Because String is not a limited type. > Why doesn't GNAT recognize it as a qualified expression? Qualified expression is not allowed for a limited types, logically. (Not yet, I think it will be a necessary step in order to continue the idea of limited aggregates. Once we allowed them, there is no reason not to allow qualified aggregates and thus limited expressions. After all aggregate is an expression. Language design bugs are always punished in the end...) > I would like to allocate dynamically something that has a constructor > function. There is no other way to create the object than with that > function and presumably it should be possible to use it with dynamic > allocation. > How can I do it? By providing a function that explicitly returns an access to T. What you are trying to do: is to enforce a custom initialization on a limited private type. That does not work in Ada, alas. It is hopeless, I am afraid. The best way I know is to make it public, removing the indefinite constraint. Otherwise you will get mounting problems crippling your design more and more, and still get no working solution. > BTW - when preparing this example I tried first with empty (null) > record, but got stuck with proper way to return an instance of T. I > remember there was some older discussion about it, but for some reason > I cannot find it and the following: > > return T'(others => <>); > > is rejected as well. > What is the proper way to create null aggregates? return (null record); if you need to specify the type, like when the formal result is class-wide, then: return X : T; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de