comp.lang.ada
 help / color / mirror / Atom feed
* Why no named case statements?
@ 2009-09-04 23:06 Britt Snodgrass
  2009-09-04 23:47 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Britt Snodgrass @ 2009-09-04 23:06 UTC (permalink / raw)


Ada allows optional names for loops and declare blocks but not for
case or if statetements. Why not, since these are also multi-line
statements that terminate with an 'end" keyword? I sometimes use loop
names to clearly indicate the purpose of the loop and have wished I
could do the same for case statements, e.g.,

Decide_This:
case Some_Variable is
...
end case Decide_This;

or similarly for long if statements:

Decide_That:
if Whatever then
...
end if Decide_That:

Such names could also be used in the outline view of an IDE like
Eclipse to support quick location of the named entity.

I suppose there was some rationale so I'm curious what it was.

- Britt



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Why no named case statements?
  2009-09-04 23:06 Why no named case statements? Britt Snodgrass
@ 2009-09-04 23:47 ` Adam Beneschan
  2009-09-05  0:29 ` Robert A Duff
  2009-09-05  8:18 ` Dmitry A. Kazakov
  2 siblings, 0 replies; 9+ messages in thread
From: Adam Beneschan @ 2009-09-04 23:47 UTC (permalink / raw)


On Sep 4, 4:06 pm, Britt Snodgrass <britt.snodgr...@gmail.com> wrote:
> Ada allows optional names for loops and declare blocks but not for
> case or if statetements. Why not, since these are also multi-line
> statements that terminate with an 'end" keyword?

Probably because for loops and blocks, the names are useful for
something else.  For loops, you can specify the loop name in an EXIT
statement (useful if you nest loops and want to exit the outer one).
For blocks, the block name can be used as part of an expanded name
(i.e. if you declare a variable V in the declaration portion of the
declare block, you can refer to Block_Name.V).  My guess is that since
the names were needed for those purposes for loops and blocks, but not
for anything else, the original language designers didn't bother to
allow them for other compound statements.


 I sometimes use loop
> names to clearly indicate the purpose of the loop and have wished I
> could do the same for case statements, e.g.,
>
> Decide_This:
> case Some_Variable is
> ...
> end case Decide_This;
>
> or similarly for long if statements:
>
> Decide_That:
> if Whatever then
> ...
> end if Decide_That:

I'd just use comments.

-- Decide_This:
   case Some_Variable is
       ...
   end case; -- Decide_This

                               -- Adam



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Why no named case statements?
  2009-09-04 23:06 Why no named case statements? Britt Snodgrass
  2009-09-04 23:47 ` Adam Beneschan
@ 2009-09-05  0:29 ` Robert A Duff
  2009-09-05  0:49   ` Adam Beneschan
  2009-09-05  8:18 ` Dmitry A. Kazakov
  2 siblings, 1 reply; 9+ messages in thread
From: Robert A Duff @ 2009-09-05  0:29 UTC (permalink / raw)


Britt Snodgrass <britt.snodgrass@gmail.com> writes:

> Ada allows optional names for loops and declare blocks but not for
> case or if statetements. Why not, since these are also multi-line
> statements that terminate with an 'end" keyword? I sometimes use loop
> names to clearly indicate the purpose of the loop...

I think loop names are for doing exits from nested loops.
If you're using them to "clearly indicate...", then I think
you are unintentionally obfuscating.  Reader thinks, "Uh, oh,
there's nested exits in here..."  But it's a false alarm.

If you want to clearly indicate the purpose of a block of
code, I think a comment is what you want, plus surrounding that
block with blank lines.  As in:

  ... -- previous code

  -- Decide this:
  case Some_Variable is
  ...
  end case;

  ... -- following code

>... and have wished I
> could do the same for case statements, e.g.,
>
> Decide_This:
> case Some_Variable is
> ...
> end case Decide_This;

I suppose you could do this:

Decide_This: begin
  case Some_Variable is
  ...
  end case Decide_This;
end Decide_This;

Actually, in GNAT there are some places that look something like this:

  case Some_Variable is
    when This =>
      Do_This: begin
        ..
      end Do_This;
    when That =>
      Do_That: begin
        ..
      end Do_That;
    ... -- and so on for hundreds of cases
  end case;

> or similarly for long if statements:
>
> Decide_That:
> if Whatever then
> ...
> end if Decide_That:
>
> Such names could also be used in the outline view of an IDE like
> Eclipse to support quick location of the named entity.
>
> I suppose there was some rationale so I'm curious what it was.

Loop names were invented so you could exit from nested ones.
Block names were invented so you could refer to nested
objects using dot notation, as in Block_Name.Local.
I suppose nobody thought of naming particular statements
as you would a procedure name.  That's my guess, as to rationale.

- Bob



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Why no named case statements?
  2009-09-05  0:29 ` Robert A Duff
@ 2009-09-05  0:49   ` Adam Beneschan
  2009-09-05  1:04     ` Robert A Duff
  0 siblings, 1 reply; 9+ messages in thread
From: Adam Beneschan @ 2009-09-05  0:49 UTC (permalink / raw)


On Sep 4, 5:29 pm, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:

> Decide_This: begin
>   case Some_Variable is
>   ...
>   end case Decide_This;
> end Decide_This;

Well, you can't say "end case Decide_This;", but I assume that was
just a typo.

But anyway, I like this solution.  It should accomplish its purpose
nicely.

                            -- Adam



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Why no named case statements?
  2009-09-05  0:49   ` Adam Beneschan
@ 2009-09-05  1:04     ` Robert A Duff
  0 siblings, 0 replies; 9+ messages in thread
From: Robert A Duff @ 2009-09-05  1:04 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> On Sep 4, 5:29�pm, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
>
>> Decide_This: begin
>> � case Some_Variable is
>> � ...
>> � end case Decide_This;
>> end Decide_This;
>
> Well, you can't say "end case Decide_This;", but I assume that was
> just a typo.

Yeah, just a cut&paste error.  Thanks for the correction.

> But anyway, I like this solution.  It should accomplish its purpose
> nicely.

Yeah, but I like the "when" example even better.

- Bob



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Why no named case statements?
  2009-09-04 23:06 Why no named case statements? Britt Snodgrass
  2009-09-04 23:47 ` Adam Beneschan
  2009-09-05  0:29 ` Robert A Duff
@ 2009-09-05  8:18 ` Dmitry A. Kazakov
  2009-09-06 12:44   ` Robert A Duff
  2 siblings, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2009-09-05  8:18 UTC (permalink / raw)


On Fri, 4 Sep 2009 16:06:12 -0700 (PDT), Britt Snodgrass wrote:

> Ada allows optional names for loops and declare blocks but not for
> case or if statetements. Why not, since these are also multi-line
> statements that terminate with an 'end" keyword? I sometimes use loop
> names to clearly indicate the purpose of the loop and have wished I
> could do the same for case statements, e.g.,
> 
> Decide_This:
> case Some_Variable is
> ...
> end case Decide_This;
> 
> or similarly for long if statements:
> 
> Decide_That:
> if Whatever then
> ...
> end if Decide_That:
> 
> Such names could also be used in the outline view of an IDE like
> Eclipse to support quick location of the named entity.
> 
> I suppose there was some rationale so I'm curious what it was.

Ada has labels:

<<Decide_This>>
   case Some_Variable is
       ...
   end case;

<<Decide_That>>
   if Whatever then
      ...
   end if:

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Why no named case statements?
  2009-09-05  8:18 ` Dmitry A. Kazakov
@ 2009-09-06 12:44   ` Robert A Duff
  2009-09-06 17:46     ` Britt
  0 siblings, 1 reply; 9+ messages in thread
From: Robert A Duff @ 2009-09-06 12:44 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> Ada has labels:
>
> <<Decide_This>>
>    case Some_Variable is
>        ...
>    end case;
>
> <<Decide_That>>
>    if Whatever then
>       ...
>    end if:

I prefer comments:

   -- Decide this.
   case Some_Variable is
       ...
   end case;

   -- Decide that.
   if Whatever then
      ...
   end if:

To me, a label is a warning that gotos are lurking around the place.

- Bob



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Why no named case statements?
  2009-09-06 12:44   ` Robert A Duff
@ 2009-09-06 17:46     ` Britt
  2009-09-07  7:27       ` Ole-Hjalmar Kristensen
  0 siblings, 1 reply; 9+ messages in thread
From: Britt @ 2009-09-06 17:46 UTC (permalink / raw)


On Sep 6, 7:44 am, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
> "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de> writes:
>
> > Ada has labels:
>
> > <<Decide_This>>
> >    case Some_Variable is
> >        ...
> >    end case;
>
> > <<Decide_That>>
> >    if Whatever then
> >       ...
> >    end if:
>
> I prefer comments:
>
>    -- Decide this.
>    case Some_Variable is
>        ...
>    end case;
>
>    -- Decide that.
>    if Whatever then
>       ...
>    end if:
>
> To me, a label is a warning that gotos are lurking around the place.

Labels are also useful as breakpoint targets if the compiler produces
associated symbolic information that can then be used by the
debugger.  Using labels for this purpose is much better (more
maintainable) than breaking on line numbers.  Such permanent
breakpoints are sometimes useful in formal white-box test procedures.
SPARK was recently changed to allow  use of Ada labels for this
purpose (breakpoints, not gotos), and the GNATPP pretty-printer now
has a new switch to format Ada labels on separate lines (as shown in
Dmitry's example).

The responses to my original question have been informative.  I think
I would still like for the next revision of Ada to allow a name on any
multi-line construct that closes with an end keyword.  This would make
the language more symmetric, I think.  It would also be useful if Ada
IDEs included both names and labels in their outline view to
facilitate source code navigation.

- Britt



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Why no named case statements?
  2009-09-06 17:46     ` Britt
@ 2009-09-07  7:27       ` Ole-Hjalmar Kristensen
  0 siblings, 0 replies; 9+ messages in thread
From: Ole-Hjalmar Kristensen @ 2009-09-07  7:27 UTC (permalink / raw)


That could be useful, but I have seen compilers produce worse code by
having labels around (like not putting variables in registers),
presumably because the analysis showed it was a potential target for a
goto.

>>>>> "B" == Britt  <britt.snodgrass@gmail.com> writes:
<snip>
    B> Labels are also useful as breakpoint targets if the compiler produces
    B> associated symbolic information that can then be used by the
    B> debugger.  Using labels for this purpose is much better (more
    B> maintainable) than breaking on line numbers.  Such permanent
    B> breakpoints are sometimes useful in formal white-box test procedures.
    B> SPARK was recently changed to allow  use of Ada labels for this
    B> purpose (breakpoints, not gotos), and the GNATPP pretty-printer now
    B> has a new switch to format Ada labels on separate lines (as shown in
    B> Dmitry's example).
<snip>
    B> - Britt

-- 
   C++: The power, elegance and simplicity of a hand grenade.



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-09-07  7:27 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-04 23:06 Why no named case statements? Britt Snodgrass
2009-09-04 23:47 ` Adam Beneschan
2009-09-05  0:29 ` Robert A Duff
2009-09-05  0:49   ` Adam Beneschan
2009-09-05  1:04     ` Robert A Duff
2009-09-05  8:18 ` Dmitry A. Kazakov
2009-09-06 12:44   ` Robert A Duff
2009-09-06 17:46     ` Britt
2009-09-07  7:27       ` Ole-Hjalmar Kristensen

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