comp.lang.ada
 help / color / mirror / Atom feed
* potential Ada feature - comments?
@ 2019-09-10 17:52 Matt Borchers
  2019-09-10 19:13 ` Dmitry A. Kazakov
  2019-09-11  9:55 ` fabien.chouteau
  0 siblings, 2 replies; 7+ messages in thread
From: Matt Borchers @ 2019-09-10 17:52 UTC (permalink / raw)


I often find myself writing the same code fragment in exception handlers.  That is, I catch and handle the specific exceptions and the cleanup code for the subprogram is identical.  I know that I can (and do) write a cleanup routine to handle this, but even so, the call to the cleanup routine must still be added to every exception case in the handler.

procedure SUBPROGRAM is
   procedure CLEANUP is
   begin
      --free resources
      ...
   end CLEANUP;
begin
   --acquire resources
   ...
exception
   when X =>
      --something for X
      cleanup;
   when Y =>
      --something for Y
      cleanup;
   when Z =>
      --something for Z
      cleanup;
   when others =>
      --general case
      cleanup;
end SUBPROGRAM;

I propose something like the following:

begin
   --acquire resources
   ...
exception
   when X =>
      --something specific for X
      ...
   when Y =>
      --something specific for Y
      ...
   when Z =>
      --something specific for Z
      ...
   when others =>
      --general case
      ...
finally
   --free resources
   ...
end SUBPROGRAM;

The code in the finally block would run immediately before the stack frame is removed for the SUBPROGRAM regardless of whether the exception is re-raised.  I would expect that the 'finally' block would only be allowed immediately after an 'exception' handler block.  The following would NOT be allowed:

begin
...
finally  --syntax error: finally must follow exception handler
  cleanup;
end SUBPROGRAM;

What do you all think?
1) It eliminates redundant code.
2) It eliminates a nested and unnecessary cleanup routine.
3) It would make me feel better when writing exception handlers.

Matt


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

* Re: potential Ada feature - comments?
  2019-09-10 17:52 potential Ada feature - comments? Matt Borchers
@ 2019-09-10 19:13 ` Dmitry A. Kazakov
  2019-09-11  0:35   ` Matt Borchers
  2019-09-11  9:55 ` fabien.chouteau
  1 sibling, 1 reply; 7+ messages in thread
From: Dmitry A. Kazakov @ 2019-09-10 19:13 UTC (permalink / raw)


On 2019-09-10 19:52, Matt Borchers wrote:
> I often find myself writing the same code fragment in exception handlers.  That is, I catch and handle the specific exceptions and the cleanup code for the subprogram is identical.  I know that I can (and do) write a cleanup routine to handle this, but even so, the call to the cleanup routine must still be added to every exception case in the handler.
> 
> procedure SUBPROGRAM is
>     procedure CLEANUP is
>     begin
>        --free resources
>        ...
>     end CLEANUP;
> begin
>     --acquire resources
>     ...
> exception
>     when X =>
>        --something for X
>        cleanup;
>     when Y =>
>        --something for Y
>        cleanup;
>     when Z =>
>        --something for Z
>        cleanup;
>     when others =>
>        --general case
>        cleanup;
> end SUBPROGRAM;

I would rather:

    procedure SUBPROGRAM is
    begin
         --acquire resources
       begin
         ...
         -- keep resources if all OK????
       exception
          when X =>
             --something for X
             raise;
          when Y =>
             --something for Y
             raise;
          when Z =>
             --something for Z
             raise;
          when others =>
             --general case
             raise;
       end;
    exception
       when others =>
          --free resources
    end SUBPROGRAM;

But my preferred method is to use a controlled object with freeing the 
resource inside finalization.

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

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

* Re: potential Ada feature - comments?
  2019-09-10 19:13 ` Dmitry A. Kazakov
@ 2019-09-11  0:35   ` Matt Borchers
  2019-09-11  7:54     ` Dmitry A. Kazakov
  2019-09-11 16:00     ` G. B.
  0 siblings, 2 replies; 7+ messages in thread
From: Matt Borchers @ 2019-09-11  0:35 UTC (permalink / raw)


Yes, I agree that a nested block is an option but it looks crappy and is it okay to complain that everything has to be unnecessarily indented to accommodate this fairly common construct?

Not everything is well designed or uses controlled objects.  In my case, I utilize a lot of legacy Ada 83 code.

procedure SUBPROGRAM is
begin
  begin
    --acquire resources
    ...
  exception
   when X =>
      --something for X
   when Y =>
      --something for Y
   when Z =>
      --something for Z
   when others =>
      --general case
  end;
  --free resources;
end SUBPROGRAM;

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

* Re: potential Ada feature - comments?
  2019-09-11  0:35   ` Matt Borchers
@ 2019-09-11  7:54     ` Dmitry A. Kazakov
  2019-09-13  1:14       ` Matt Borchers
  2019-09-11 16:00     ` G. B.
  1 sibling, 1 reply; 7+ messages in thread
From: Dmitry A. Kazakov @ 2019-09-11  7:54 UTC (permalink / raw)


On 2019-09-11 02:35, Matt Borchers wrote:
> Yes, I agree that a nested block is an option but it looks crappy and is it okay to complain that everything has to be unnecessarily indented to accommodate this fairly common construct?

"Finally" is IMO misleading. E.g. this would be wrong:

    declare
       File : File_Type;
    begin
       Open (File, ...);
       ...
    finally
       Close (File);
    end;

The problem of exception handlers sharing parts of code does exist, but 
"finally" is no solution because it is too crude. In practice there are 
2-3 different variants of finalization upon the subroutine exit. E.g. 
with exception vs. without exception.

I think that the solution must be based on contracts, exception 
contracts in particular to make it possible to declare resources as 
local and statically checking their releasing.

It also should be close to the place where the resource is claimed. One 
of many wrongs of "finally" is that it appears at the end of the block. 
It should rather be right after the call to Open, in my above. E.g.

    declare
       File : File_Type;
    begin
       Open (File, ...); -- Tell here to Close (File) afterwards!
       ...
    end;

And, of course, as a proponent of OO and strongly typed approach, I 
would rather have some syntax sugar for light-weight ad-hoc controlled 
wrapper types. E.g. to stuff File_Type into and delegate all operations 
of File_Type to it without writing 2k lines of brain-dead code.

P.S. Ada 83 is fine, but any language change would make it Ada 20XY anyway.

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

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

* Re: potential Ada feature - comments?
  2019-09-10 17:52 potential Ada feature - comments? Matt Borchers
  2019-09-10 19:13 ` Dmitry A. Kazakov
@ 2019-09-11  9:55 ` fabien.chouteau
  1 sibling, 0 replies; 7+ messages in thread
From: fabien.chouteau @ 2019-09-11  9:55 UTC (permalink / raw)


On Tuesday, September 10, 2019 at 7:52:56 PM UTC+2, Matt Borchers wrote:
> What do you all think?
> 1) It eliminates redundant code.
> 2) It eliminates a nested and unnecessary cleanup routine.
> 3) It would make me feel better when writing exception handlers.


I think it looks interesting.

You can also share this idea here if you want: https://github.com/AdaCore/ada-spark-rfcs


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

* Re: potential Ada feature - comments?
  2019-09-11  0:35   ` Matt Borchers
  2019-09-11  7:54     ` Dmitry A. Kazakov
@ 2019-09-11 16:00     ` G. B.
  1 sibling, 0 replies; 7+ messages in thread
From: G. B. @ 2019-09-11 16:00 UTC (permalink / raw)


Matt Borchers <mattborchers@gmail.com> wrote:
> Yes, I agree that a nested block is an option but it looks crappy and is
> it okay to complain that everything has to be unnecessarily indented to
> accommodate this fairly common construct?
> 

Hide the nest and have the compiler rewrite?

exception 
   with exit =>
       -- deferred statements
   when X =>
       -- ...
   when others =>
       -- ...
end



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

* Re: potential Ada feature - comments?
  2019-09-11  7:54     ` Dmitry A. Kazakov
@ 2019-09-13  1:14       ` Matt Borchers
  0 siblings, 0 replies; 7+ messages in thread
From: Matt Borchers @ 2019-09-13  1:14 UTC (permalink / raw)


I am not a proponent of OOP in general but I do think that some of the principles of it can lead to better software.

I like the idea of defining how resources should be returned at the point where they are acquired, but that does not define WHEN that should happen unless it is assumed that "going out of scope" is always the proper time.  Ada is still a procedural language such that, outside of using Controlled types, one can read the lines of a sub-program and know the exact order of what is going to be executed.

My "inconvenience" is that there is no place in the syntax to execute code after the sub-program's main exception handler except to create a unfortunate anonymous block only to provide that space following it.  Returning resources is only one example of a use-case for this syntax.  I think there are other reasons this would be helpful.


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

end of thread, other threads:[~2019-09-13  1:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-10 17:52 potential Ada feature - comments? Matt Borchers
2019-09-10 19:13 ` Dmitry A. Kazakov
2019-09-11  0:35   ` Matt Borchers
2019-09-11  7:54     ` Dmitry A. Kazakov
2019-09-13  1:14       ` Matt Borchers
2019-09-11 16:00     ` G. B.
2019-09-11  9:55 ` fabien.chouteau

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