comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: why "unconstrained subtype in component declaration" while said component given default value ?
Date: Sun, 27 May 2018 09:45:48 +0200
Date: 2018-05-27T09:45:48+02:00	[thread overview]
Message-ID: <pednnc$g7p$1@dont-email.me> (raw)
In-Reply-To: <2d717805-f6a4-4de9-bf4a-d45037034f96@googlegroups.com>

On 05/26/2018 11:40 PM, Jere wrote:
> 
> Consider for a second that you are the compiler and you need to
> allocate a variable:
> 
> My_Variable : A(10);
> 
> What size (in bytes) do you need to allocate for the variable?  Remember
> that the function Give_String can be exceedingly complex or even a
> function from a shared library.  How do you at compile time determine
> the size of that record type?

There's more to it than that. Consider

type R (L : Positive) is record
    S : String (1 .. L);
end record;

V : R (L => Some_Function);

This is valid Ada, but the compiler cannot tell the size in bytes to allocate 
for V. However, the compiler can generate code to determine the size at run time.

In its most basic form, this concept is

type R is record
    S : String := Some_Function;
end record;

V : R;

which is not much different from the valid declaration

S : String := Some_Function;

so it seems to me that the invalid form could be allowed. The latter declaration 
was invalid in Ada 83; on more than one occasion I wrote

C : constant String := Some_Function;
S : String (C'range) := C;

to get around this restriction (and hoped the compiler would be smart enough to 
see that there were no other references to C, and not keep 2 copies of the value).

-- 
Jeff Carter
"Have you gone berserk? Can't you see that that man is a ni?"
Blazing Saddles
38

  parent reply	other threads:[~2018-05-27  7:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-26 21:03 why "unconstrained subtype in component declaration" while said component given default value ? Mehdi Saada
2018-05-26 21:40 ` Jere
2018-05-26 22:11   ` Mehdi Saada
2018-05-26 23:55     ` Jere
2018-05-27  0:14       ` Jere
2018-05-27  0:29       ` Mehdi Saada
2018-05-27  0:45         ` Mehdi Saada
2018-05-27 17:15         ` Shark8
2018-05-27 17:30           ` AdaMagica
2018-05-27 18:17             ` Shark8
2018-05-29  1:28               ` AdaMagica
2018-05-29 17:20                 ` Mehdi Saada
2018-05-27  7:45   ` Jeffrey R. Carter [this message]
2018-05-26 23:41 ` Shark8
2018-05-27  7:07 ` Simon Wright
2018-05-27 17:22   ` Shark8
2018-05-27 12:24 ` Mehdi Saada
2018-05-29 22:28   ` Randy Brukardt
replies disabled

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