comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: A suggestion for resource management
Date: Sat, 21 Aug 2010 23:34:28 +0300
Date: 2010-08-21T23:34:28+03:00	[thread overview]
Message-ID: <8darikF1b0U1@mid.individual.net> (raw)
In-Reply-To: <8762z4gcoi.fsf@mid.deneb.enyo.de>

Florian Weimer wrote:
> Here's a proposal for a resource management pragma, tentatively called
> pragma Scoped.  It is similar to the scope(exit) directive in D, the
> C++ scope guard idom, and (to a lesser degree) Go's rescue statement.
> 
> The syntax is:
> 
>    pragma Scoped (procedure_call_statment);

I strongly dislike the idea of using "pragma" to introduce such 
non-obvious flow of control.

Flow of control should be clearly represented in the syntactic 
structure, with textual order matching execution order. The current 
exception-handling syntax is good in that respect.

> Regarding dynamic semantics: When a Scoped pragma which is part of a
> handled_sequence_of_statements is executed or when it is elaborated as
> part of a declarative_part, the procedure_name or procedure_prefix and
> actual parameters of the procedure_call_statment are evaluated.

... and, if I understand your suggestion, the evaluated parameters are 
saved somewhere, in case they are needed for the later call. I think 
this can cause large run-time overheads, as well as confusion and 
errors. How "deeply" are the parameters saved? The semantics will depend 
on whether a type is passed by value or by access, right? Also, saved 
parameters of access type may become unusable (hanging pointers) if the 
statement_sequence deallocates the accessed object. The state of the 
relevant objects may also change in some other non-obvious but 
significant ways that invalidate the saved parameter values.

You did not discuss if "pragma Scoped" could be executed conditionally, 
as in:

    Do_Something ( ... Success => Ok);
    if Ok then
       pragma Scoped (Undo_Something (...));
    end if;

Is this included in your suggestion?

> The example at the end of section B.4 could use the Scoped pragma in
> this way:
> 
>           procedure Test_External_Formats is
>              ...
>              COBOL_File : File_Type;
>              ...
> 
>           begin
>              Open (COBOL_File, Name => "Some_File");
>              pragma Scoped (Close (COBOL_File));
> 
>              loop
>                ...
>           exception
>              when End_Error => ...
>           end Test_External_Formats;

If I understand your suggestion correctly, more or less the same 
behaviour can be achieved in current Ada by a statement block:

    procedure Test_External_Formats is
       ...
       COBOL_File : File_Type;
       ...
    begin
       Open (COBOL_File, Name => "Some_File");
       begin
          loop
             ...
          end loop;
       exception
          when End_Error => ...
       end;
       Close (COBOL_File);
    end Test_External_Formats;

I find that form clear and brief enough, although I admit that nesting 
depth may become awkwardly large (the remedy is to split the procedure).

I'm not sure if your suggestion is that the "pragma" procedure call 
(here, Close (COBOL_File)) should be performed also if an exception is 
propagated from the exception handler of the 
handled_sequence_of_statements. My version above does not perform Close 
in that case. If Close should be performed in that case, I would very 
much prefer new syntax for a "finally" structure placed at the end of 
the handled_sequence_of_statements, after the exception handler if any, 
over the "pragma" suggestion.

In summary, the present language already has tools for this need, either 
finalization (as Dmitry suggested) or the nested block shown above. 
Cases where the present tools are not ideal could be covered by a new 
"finally" construct.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  parent reply	other threads:[~2010-08-21 20:34 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-21 16:20 A suggestion for resource management Florian Weimer
2010-08-21 19:07 ` Dmitry A. Kazakov
2010-08-21 19:47   ` Florian Weimer
2010-08-21 20:53     ` Dmitry A. Kazakov
2010-08-21 21:09       ` Florian Weimer
2010-08-22  6:40         ` Dmitry A. Kazakov
2010-08-23 23:22         ` Randy Brukardt
2010-08-21 20:34 ` Niklas Holsti [this message]
2010-08-21 21:01   ` Florian Weimer
2010-08-22 10:53     ` Niklas Holsti
2010-08-22 15:29       ` Florian Weimer
2010-08-22 16:12       ` Florian Weimer
2010-08-23 12:25         ` Niklas Holsti
2010-09-04 19:09           ` Florian Weimer
2010-09-07 10:14             ` Niklas Holsti
2010-08-22 11:32     ` Georg Bauhaus
2010-08-23 23:37       ` Randy Brukardt
2010-08-22 13:09 ` stefan-lucks
2010-08-22 14:30   ` Florian Weimer
2010-08-22 15:09   ` Florian Weimer
replies disabled

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