comp.lang.ada
 help / color / mirror / Atom feed
* Re: ML-like alternatives to out parameters for functions
@ 2003-04-02  0:23 Alexandre E. Kopilovitch
  0 siblings, 0 replies; 15+ messages in thread
From: Alexandre E. Kopilovitch @ 2003-04-02  0:23 UTC (permalink / raw)
  To: comp.lang.ada

Frank J. Lhota (NOSPAM.lhota.adarose@verizon.net) wrote:
>In most cases, parameter modes are an aid to both the compiler and the
>programmer, telling them when we can assume that an actual parameter could
>be altered and when its state will remain the same. In the case of the
>Random function, however, the parameter mode is a lie. The function
>specification indicates that the Gen parameter is not modified by a call to
>Random, even though we all know that it does indeed get modified. This is an
>inelegant kludge,
>...
Yes, it certainly looks like a kludge. This is even more annoying because (as
it was once explained in Ada-Comment list) there are good reasons for that
solution for the Random (some tasking issues, I recall).
  It seems that for such cases, a real cure should not be permission of IN OUT
parameters for functions (this is a separate issue, and it is, perhaps, locked
for ages... it deserves at least a separate section at the AdaPower site -:) .
  Let's introduce new parameter mode, say, PASS mode, which is applicable for
access parameters only. With this mode one can pass an access parameter to a
subroutine, explicitly stating that this is access for update, while IN mode
will not give permission for update.


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




^ permalink raw reply	[flat|nested] 15+ messages in thread
* ML-like alternatives to out parameters for functions
@ 2003-03-17 22:17 Mark Lorenzen
  2003-03-17 21:47 ` Stephen Leake
                   ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: Mark Lorenzen @ 2003-03-17 22:17 UTC (permalink / raw)


There has lately been some discussions about Ada's missing
capabilities for specifying out parameters for functions. Apparently
this is a needed feature and an AI (AI-323) has been issued.

An out parameter can be used when the validity of the result of
evaluating a function is not always well-defined. For example:

function Calculate (Argument : in AT; Result_Valid : out Boolean) return RT;


Use of the function could be as:

Result := Calculate (Arg, Valid);
if Valid then
  .. do stuff with Result ..
else
  .. handle error ..
end if;


It seems to me that using a discriminated record type as return type
in order to mimic ML's Option datatype could solve such a (frequently)
occurring problem. But of course I am not the first to come up with
this suggestion, so what is wrong with it? Is it too slow?

The package spec is shown below and should be self-explanatory.

Regards,
- Mark Lorenzen

generic
   type Value_Type is private;
package Optional_Value is

   type Optional_Value_Type is private;

   Undefined_Value : exception;

   function Some
     (Value : in Value_Type)
     return Optional_Value_Type;

   function None
     return Optional_Value_Type;

   -- May raise Undefined_Value;
   function Value_Of
     (Element : in Optional_Value_Type)
     return Value_Type;

   function Is_Defined
     (Element : in Optional_Value_Type)
     return Boolean;

private

   type Optional_Value_Type (Defined : Boolean := False) is
      record
        case Defined is
           when True  =>
              Value : Value_Type;
           when False =>
              null;
        end case;
      end record;

end Optional_Value;



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

end of thread, other threads:[~2003-04-02  0:23 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-02  0:23 ML-like alternatives to out parameters for functions Alexandre E. Kopilovitch
  -- strict thread matches above, loose matches on Subject: below --
2003-03-17 22:17 Mark Lorenzen
2003-03-17 21:47 ` Stephen Leake
2003-03-17 23:34   ` Mark Lorenzen
2003-03-18  3:54     ` John R. Strohm
2003-03-18  8:59     ` Preben Randhol
2003-03-18 21:09     ` tmoran
2003-03-18 17:04   ` Frank J. Lhota
2003-03-18 18:52     ` Mark Lorenzen
2003-03-18 19:16       ` Frank J. Lhota
2003-04-01  3:39         ` Robert I. Eachus
2003-04-01 14:51           ` Frank J. Lhota
2003-03-18  8:37 ` Dmitry A. Kazakov
2003-03-18  9:07 ` Preben Randhol
2003-03-19  7:31 ` Mark Biggar

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