From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a00006d3c4735d70 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-02-26 00:45:08 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!news.tele.dk!news.tele.dk!small.news.tele.dk!uninett.no!ntnu.no!not-for-mail From: Preben Randhol Newsgroups: comp.lang.ada Subject: Re: In-Out Parameters for functions Date: Thu, 26 Feb 2004 08:45:07 +0000 (UTC) Organization: PVV Message-ID: References: <1077634311.254581@master.nyc.kbcfp.com> <1077718871.47635@master.nyc.kbcfp.com> <54cp3095jmv8s17h63d4bjdus0tec7l7pt@jellix.jlfencey.com> <1077721343.481619@master.nyc.kbcfp.com> <1077724400.221032@master.nyc.kbcfp.com> <1077727496.631820@master.nyc.kbcfp.com> <103q2cf890pus79@corp.supernews.com> <1077744815.17965@master.nyc.kbcfp.com> NNTP-Posting-Host: k-083152.nt.ntnu.no X-Trace: tyfon.itea.ntnu.no 1077785107 13687 129.241.83.152 (26 Feb 2004 08:45:07 GMT) X-Complaints-To: usenet@itea.ntnu.no NNTP-Posting-Date: Thu, 26 Feb 2004 08:45:07 +0000 (UTC) User-Agent: slrn/0.9.8.0 (Linux) Xref: archiver1.google.com comp.lang.ada:5825 Date: 2004-02-26T08:45:07+00:00 List-Id: On 2004-02-25, Hyman Rosen wrote: > Randy Brukardt wrote: >> "Hyman Rosen" wrote >>>Where do you find permission for Ada to not evaluate one >>>operand of an "and" or "or" expression? >> >> 11.6(5), of course. > > For the particular case of sqrt(x), where we're talking > about Ada's built-in sqrt, and the only possible side-effect > is for it to raise an exception. But if instead we have > (gnurble(x) > 0.5 and b), and the compiler knows that b is > false but has no information about what gnurble does, it > must call gnurble(x) even though the result is irrelevant. Or you have to do: value := gnuble(x); if value > 0.5 and b then to be sure that any side effects are performed. with Ada.Text_IO; use Ada.Text_IO; procedure eval is flag : Boolean := false; function gnurble(value : Float) return Float is begin Put_Line ("I was called"); return value + 99.0; end gnurble; begin if flag and gnurble (1.0) > 2.0 then -- if gnurble (1.0) > 2.0 and flag then -- or this case -- if false and gnurble (1.0) > 2.0 then -- or this -- if gnurble (1.0) > 2.0 and false then -- or this Put_Line ("True"); else Put_Line ("False"); end if; end eval; For all these cases I get that the gnurble function was called and the if statment is of course false. Even with optimisation. However if you do this: with Ada.Text_IO; use Ada.Text_IO; procedure eval is flag : Boolean := false; function gnurble(value : Float) return Float is begin flag := true; -- Added a side effect Put_Line ("I was called"); return value + 99.0; end gnurble; begin if flag and gnurble (1.0) > 2.0 then -- Case 1 -- if gnurble (1.0) > 2.0 and flag then -- Case 2 Put_Line ("True"); else Put_Line ("False"); end if; end eval; The result is: Case 1: % ./eval I was called True Case 2: % ./eval I was called True Wheras if you do: if flag and then gnurble (1.0) > 2.0 then then gnurble is never called of course. Compiler GNAT 3.15p -- "Saving keystrokes is the job of the text editor, not the programming language."