comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
Subject: Re: ML-like alternatives to out parameters for functions
Date: Tue, 18 Mar 2003 19:16:34 GMT
Date: 2003-03-18T19:16:34+00:00	[thread overview]
Message-ID: <mgKda.72158$gi1.33350@nwrdny02.gnilink.net> (raw)
In-Reply-To: m31y14bj83.fsf@valhal.vikingnet

"Mark Lorenzen" <mark.lorenzen@ofir.dk> wrote in message
news:m31y14bj83.fsf@valhal.vikingnet...
> But again this relies on side-effects on it's parameters. I would go
> for a solution like this:
>
> function Random (Gen : in Generator) return Random_Continuation
>
> where
>
> type Random_Continuation is
>   record
>     Gen   : Generator;
>     Value : Uniformly_Distributed;
>   end record;

There are two problems with this approach. One is that it assumes that
Generator is not a limited type; currently Generator is limited. But even if
we change the declaration of Generator, this version of Random places a
heavy burden on the caller. In almost all cases, users of this function
simply want to get the next random number and update the generator. With
this version of Random, this can be accomplished only be doing something
like this:

   declare
      Temp : constant Random_Continuation := Random( Gen );
   begin
      Gen   := Temp.Gen;
      Value := Temp.Value;
   end;

Do you really want to require this for every call to Random?

Another possible solution would be to implement Random as a procedure, i.e.

   procedure Random( Gen   : in out Generator;
                     Value :    out Uniformly_Distributed );

This approach would probably the best approach given the current Ada 95
restrictions, since it specifies precisely how Random works: it updates the
generator and returns a value. For ease of use, however, it would be hard to
beat the function solution with the "in out" parameter (if it only were
legal Ada!).





  reply	other threads:[~2003-03-18 19:16 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-17 22:17 ML-like alternatives to out parameters for functions 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 [this message]
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
  -- strict thread matches above, loose matches on Subject: below --
2003-04-02  0:23 Alexandre E. Kopilovitch
replies disabled

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