comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Dynamic allocation of unconstrained types
Date: Wed, 30 Sep 2009 16:56:41 +0200
Date: 2009-09-30T16:56:42+02:00	[thread overview]
Message-ID: <1qy4b03u1mtz1.1ri6symgrtrgo$.dlg@40tude.net> (raw)
In-Reply-To: dd95bb9b-7604-4347-a964-2108502cf4c8@37g2000yqm.googlegroups.com

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



  parent reply	other threads:[~2009-09-30 14:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-30 14:29 Dynamic allocation of unconstrained types Maciej Sobczak
2009-09-30 14:50 ` Robert A Duff
2009-09-30 14:54 ` Adam Beneschan
2009-09-30 18:30   ` Jeffrey R. Carter
2009-09-30 19:15     ` Adam Beneschan
2009-09-30 14:56 ` Dmitry A. Kazakov [this message]
2009-09-30 15:03   ` Adam Beneschan
replies disabled

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