comp.lang.ada
 help / color / mirror / Atom feed
* Mutable parameter of a function (long ago was: Re: What evil would happen?)
@ 2004-01-02 17:55 Alexandre E. Kopilovitch
  2004-01-03  1:56 ` Robert I. Eachus
  0 siblings, 1 reply; 12+ messages in thread
From: Alexandre E. Kopilovitch @ 2004-01-02 17:55 UTC (permalink / raw)
  To: comp.lang.ada

Quite long (about a half of year) ago Robert I. Eachus wrote about the rule,
which forbids IN OUT mode for parameters of a function:

>...
> But the rule still won't change, and the reason is not just "politics." 
>   This is just one area of the Ada programming culture that seems to rub 
> foreigners (to Ada) the wrong way.  If you are used to Ada, the current 
> Ada idioms for the few cases that seem to some people to be problems are 
> natural to you.
>
> Since I have done a lot of work on random number generators, the Ada 
> idiom now seems natural to me:
>
> function Random(Gen: in Generator) return ...
>   Local: Generator;
>   for Local'Address use Gen'Address;
> begin
>    ...
> end Random;
>
> Unlike RBKD, I did strongly argue for an in out parameter mode for 
> functions in Ada 9X.  But in doing so I knew that it would be misused 
> more than it would be correctly used.  By now with good implementations 
> of Unbounded_String, most of the real needs for functions with in out 
> parameters have gone away.  You can always declare a procedure with an 
> out parameter of Unbounded_String.  So if the question is reraised I 
> will vote against it.  And I will also vote in favor of a generic that 
> provides the equivalent of Unbounded_String for all array types, 
> including multidimensional array types.

While I was generally satisfied with that particular idiom for the need,
as well as with overall explanations given around it, nevertheless a feeling
remained that some final bit is still missing. And now I came to an idea
about that missing bit, that is, which thing is actually missing, and how it
can be provided.

So, I think that the primary problem with that idiom is that it may be
unknown for a programmer, and the secondary problem is that it isn't always
easily recognizable. In general, the missing thing is a reference point for
this idiom.

Therefore it seems suitable to introduce a pragma for this purpose - to
provide a reference point. This pragma will not affect the code, it will
merely state that the idiom is used for particular parameter of the
subroutine:

  pragma Mutable(subroutine-name, parameter-name); -- or Mutable_Parameter

This pragma checks for two things: 1) the parameter to which it is applied
is in IN mode; 2) the idiom is used for this parameter, that is, there is
a local variable (of appropriate type) in the subroutine, and the Address
attribute for this variable is defined and points to the parameter. If this
check is failed then compiler should complain with long diagnostic message,
which contains these rules (thus effectively explaining the idiom).

What is good with this pragma? Two points: 1) it provides explicit reference
for the idiom - if not within the language manual then at least within a
compiler's documentation (if this pragma will be implementation-defined);
2) this pragma will respond to quite justified Robert Dewar's complaint -
that we can modify a parameter of a function, but can't say that explicitly
in the function's specs.
 
Certainly, this pragma should be optional, that is, the idiom can be used
without this pragma - for obvious compatibility reasons. Although it may be
made required for use of the idiom (that is, for assignment to a variable,
which address is defined by the Address attribute applied to an IN mode
parameter) in subroutines that clearly rely upon Ada0Y features.



Alexander Kopilovitch                      aek@vib.usr.pu.ru
Saint-Petersburg
Russia




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

end of thread, other threads:[~2004-01-06  9:32 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-02 17:55 Mutable parameter of a function (long ago was: Re: What evil would happen?) Alexandre E. Kopilovitch
2004-01-03  1:56 ` Robert I. Eachus
2004-01-03 11:18   ` Dmitry A. Kazakov
2004-01-04  0:58     ` Robert I. Eachus
2004-01-04 12:07       ` Dmitry A. Kazakov
2004-01-05  3:17         ` Alexandre E. Kopilovitch
2004-01-05  9:55           ` Dmitry A. Kazakov
2004-01-05 16:19             ` Robert I. Eachus
2004-01-06  3:04             ` Randy Brukardt
2004-01-06  9:32               ` Dmitry A. Kazakov
2004-01-06  3:01         ` Randy Brukardt
2004-01-06  9:26           ` Dmitry A. Kazakov

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