From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Ada case-statement
Date: Wed, 14 Mar 2018 20:57:54 -0400
Date: 2018-03-14T20:57:54-04:00 [thread overview]
Message-ID: <p8cgeh$1v7d$1@gioia.aioe.org> (raw)
In-Reply-To: p8bnbi$n0f$1@gioia.aioe.org
On 3/14/2018 1:49 PM, Dmitry A. Kazakov wrote:
> On 2018-03-14 18:35, Stephen Davies wrote:
>> I guess that this has probably been considered before,
>> but it would be nice if Ada allowed sub-cases, with
>> coverage checking of the appropriate alternatives.
>> This seems like a simpler change than some other recent
>> ones (e.g. conditional expressions), and would remove
>> one problem with not having non-contiguous subtypes.
>> For example:
>>
>> case Calculate_Day is
>> when Mon .. Fri =>
>> ...
>> when Weekend : Sat | Sun =>
>> ...
>> case Weekend is
>> when Sat =>
>> ...
>> when Sun =>
>> ...
>> end case;
>> end case;
>>
>> Ok, I know this isn't going to happen, I just wanted to
>> put it out there anyway.
>
> Great idea, would be useful for variant records and exception handlers too.
>
The variant record case might justify a language change. For actual
case statements I do:
case Calculate_Day is
when Mon .. Fri =>
...
when Sat | Sun =>
...
case Calculate_Day is
when Sat =>
...
when Sun =>
...
when others => null; -- or raise Constrain_Error depending
-- on style.
end case;
end case;
If Calculate_Day is a non-trivial function you might want to do:
The_Day: Day := Calculate_Day
...
case The_Day is
when Mon .. Fri =>
...
when Sat | Sun =>
...
case The_Day is
when Sat =>
...
when Sun =>
...
when others => null; -- or raise Constrain_Error depending
-- on style.
end case;
end case;
It would be nice to allow compilers to allow the when others to be
omitted based on what it knows about the value of The_Day but all that
is syntactic sugar. Or you could allow something like:
case The_Day in Sat..Sunday is -- very bad, better syntax needed.
What I usually end up doing in complex cases is to do:
declare
The_Day: constant Day := Calculate_Day;
begin
if The_Day in Mon..Fri then
...
elsif The+Day = Saturday then
...
else -- Day is Sunday
...
end if;
end;
Why? It seems to me that every time I end up with a complicated case, it
is in the critical path of task scheduling. (Or maybe I never need to
look at others.) The compiler probably will convert to an if statement,
but by making the if statement explicit, I get to choose which case gets
evaluated first. (The compiler should also do any necessary branch tail
merging.) I might even choose to optimize for speed to get rid of the
branches in the code. On today's processors, branches can cost a lot
more than other instructions.)
next prev parent reply other threads:[~2018-03-15 0:57 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-14 17:35 Ada case-statement Stephen Davies
2018-03-14 17:49 ` Dmitry A. Kazakov
2018-03-15 0:57 ` Robert I. Eachus [this message]
2018-03-15 3:10 ` Dan'l Miller
2018-03-15 5:54 ` J-P. Rosen
2018-03-15 7:56 ` Niklas Holsti
2018-03-15 12:21 ` Dan'l Miller
2018-03-15 17:22 ` Mill processor (Was: Re: Ada case-statement) Niklas Holsti
2018-03-15 21:50 ` Ada case-statement Randy Brukardt
2018-03-14 22:22 ` Mehdi Saada
2018-03-14 23:16 ` Randy Brukardt
2018-03-15 5:04 ` gautier_niouzes
2018-03-15 7:50 ` Jacob Sparre Andersen
2018-03-15 22:05 ` Randy Brukardt
2018-03-15 8:37 ` Dmitry A. Kazakov
2018-03-15 22:20 ` Randy Brukardt
2018-03-16 8:54 ` Dmitry A. Kazakov
2018-03-16 23:49 ` Randy Brukardt
2018-03-17 7:59 ` Dmitry A. Kazakov
2018-03-15 15:37 ` Stephen Davies
2018-03-15 16:33 ` J-P. Rosen
2018-03-15 17:01 ` Dmitry A. Kazakov
2018-03-15 18:41 ` Shark8
2018-03-15 21:12 ` Jeffrey R. Carter
2018-03-18 5:41 ` Robert I. Eachus
2018-03-18 6:57 ` Spiros Bousbouras
2018-03-18 9:17 ` Jeffrey R. Carter
2018-03-18 12:53 ` Simon Wright
2018-03-15 18:50 ` Jere
2018-03-15 20:40 ` Anh Vo
2018-03-15 22:24 ` Randy Brukardt
2018-03-16 9:53 ` Stephen Davies
2018-04-03 17:56 ` marciant
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox