comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Raise expressions from AARM.
Date: Sat, 24 Feb 2024 12:39:43 +0200	[thread overview]
Message-ID: <l3tvbfF913vU1@mid.individual.net> (raw)
In-Reply-To: <urce58$14j88$1@dont-email.me>

On 2024-02-24 11:50, Blady wrote:
> Hello,
> 
> AARM Ada 2022 section 11.3 presents some uses of raise expressions 
> including this one:
> (http://www.ada-auth.org/standards/22aarm/html/AA-11-3.html)
> 
> 2.a.10/4        ...
> 
>                  B : Some_Array := (1, 2, 3, others => raise 
> Not_Valid_Error);
> 
> What could be the use cases?


The point of these examples (which are only in the discussion 
annotation, not in the normative standard) is to discuss what is 
syntactically legal and why. The examples need not make practical sense.


> My guess: whatever the size of Some_Array (greater than 3), B is 
> elaborated but raises Not_Valid_Error when accessing component beyond 
> position 3:


No. A raise-expression is not a value that can be stored in an array or 
passed around; its evaluation raises an exception /instead/ of yielding 
a value.

In this example, if the evaluation of the array aggregate that 
initializes B evaluates the expression supplied for the "others" choice, 
this evaluation will raise Not_Valid_Error and disrupt the 
initialization of B.

It is not clear to me if the RM requires the evaluation of the "others" 
expression if there are no "other" indices. Experimenting with GNAT 
(Community 2019) shows that if the Some_Array type has 'Length = 3, the 
exception is not raised (so the "others" value is not evaluated), while 
if the 'Length is greater than 3 the exception is raised.


> type Some_Array is array (Positive range 1..10) of Natural;
> ...
> B : Some_Array := (1, 2, 3, others => raise Not_Valid_Error);


That should raise Not_Valid_Error during the initialization of B.


> ...
> begin
> X := B (2); -- OK
> X := B (6); -- raises Not_Valid_Error
> end;
> 
> Is it correct?


No.

> 
> NB: GNAT 13.2 issues a compilation error:
>          >>> error: "others" choice not allowed here
> see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113862


Interesting. GNAT Community 2019 accepted it.

  parent reply	other threads:[~2024-02-24 10:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-24  9:50 Raise expressions from AARM Blady
2024-02-24 10:39 ` Jeffrey R.Carter
2024-02-25 11:09   ` Blady
2024-02-24 10:39 ` Niklas Holsti [this message]
2024-02-25 11:23   ` Blady
2024-02-26 20:01     ` Niklas Holsti
replies disabled

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