From: Victor Porton <porton@narod.ru>
Subject: Re: Rationale for this not to compile?
Date: Fri, 25 Jul 2014 15:19:39 +0300
Date: 2014-07-25T15:19:39+03:00 [thread overview]
Message-ID: <lqti0p$bae$1@speranza.aioe.org> (raw)
In-Reply-To: lqspuv$bih$1@loke.gir.dk
Randy Brukardt wrote:
>
> "Adam Beneschan" <adambeneschan@gmail.com> wrote in message
> news:1cb91e6a-9e49-4bc8-bf4a-67a16a00e3a0@googlegroups.com...
>>On Thursday, July 24, 2014 1:53:32 PM UTC-7, Victor Porton wrote:
>>> Is there any rationale why this does not compile?
>>
>>A generic instantiation is equivalent to "the instance declaration ...
>>immediately followed
>>by the instance body, both at the place of the instantiation." (12.3(12))
>>Since the body
>>could contain code that depends on the representation of X already being
>>determined,
>>but it hasn't yet, the language makes this illegal. At least I think
>>that's the rationale.
>>AARM section 13.14, which discusses the freezing rules, explains in more
>>detail why
>>types have to be completely defined before they're used in certain cases
>>(see
>> http://www.ada-auth.org/standards/12aarm/html/AA-13-14.html).
>
> That's a pretty good explanation.
>
> We (the ARG) have spent dozens of hours attempting to find a way to allow
> something like the OPs example. (There's something like 10 AIs associated
> with that.) Each solution had some sort of fatal flaw (implementability,
> maintenance problems, too hard to use, etc.)
>
> The best solution is the put the instance into a child package; one of the
> reasons that we didn't adopt any of the ugly solutions is that using a
> child package often works fine and an ugly solution doesn't seem worth it
> in that case.
>
> In the special case where the generic has little implementation of it's
> own (a signature package), one can use a formal incomplete type as the
> formal type; it does allow an instantiation like the one the OP wrote.
> Specifically:
>
> package OK_Test is
>
> generic
> type T;
> package Gen is
> end Gen;
>
> type X is private;
>
> package Instance is new Gen(X);
>
> private
>
> type X is null record;
>
> end OK_Test;
>
> is legal. But the use of the formal incomplete type strongly restricts
> what you can do in the generic unit (because it's an incomplete type, and
> those allow very limited uses).
>
> One of my pet projects (that I don't have any time to follow up on!) is to
> create a useful container where the element type is a formal incomplete
> type. I think that the container would have to manage elements that live
> elsewhere (unlike Ada.Containers) -- since element creation isn't
> possible, but this would allow a container of limited elements, and of
> course would allow instantiation by a private type as here.
>
> Randy.
>
>
>
>
>
>
>> If it compiled, it would
>> be probably useful in practice with a real example similar to the below
>> toy
>> example (as to make the type T not private, I must create an *.Internal.*
>> package to serve as a "private repository" for types like T, as real
>> private
>> types cannot be used). It is called a hack.
>>
>> gnatmake -q -c -gnatc -u -P/home/porton/t/default.gpr test.ads
>> test.ads:10:32: premature use of private type
>> gnatmake: "/home/porton/t/test.ads" compilation error
>>
>> package Test is
>>
>> generic
>> type T is limited private;
>> package Gen is
>> end Gen;
>>
>> type X is private;
>>
>> package Instance is new Gen(X);
>>
>> private
>> type X is null record;
>> end Test;
Sorry for a stupid question:
What is the difference of:
generic
type T
package Gen is
end Gen;
and
generic
type T is private
package Gen is
end Gen;
--
Victor Porton - http://portonvictor.org
next prev parent reply other threads:[~2014-07-25 12:19 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-24 20:53 Rationale for this not to compile? Victor Porton
2014-07-24 21:36 ` Adam Beneschan
2014-07-25 5:29 ` Randy Brukardt
2014-07-25 12:19 ` Victor Porton [this message]
2014-07-25 12:35 ` AdaMagica
2014-07-25 14:40 ` Robert A Duff
2014-07-25 19:45 ` Randy Brukardt
2014-07-25 20:33 ` Robert A Duff
2014-07-25 13:18 ` Victor Porton
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox