comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: why this construct causes aliasing problem in 2012 with functions allowing In Out?
Date: Wed, 20 Jun 2012 07:56:12 -0700 (PDT)
Date: 2012-06-20T07:56:12-07:00	[thread overview]
Message-ID: <2adb0da6-9b6e-4514-b703-f0a6261ecc5b@googlegroups.com> (raw)
In-Reply-To: <jrs1bd$eth$1@speranza.aioe.org>

On Wednesday, June 20, 2012 1:24:11 AM UTC-7, Nasser M. Abbasi wrote:
> looking at http://www.slideshare.net/AdaCore/ada-2012
> 
> (typing it myself looking at the screen)
> 
> --------------------------
> function change(x,y : in out integer) return integer is
>   begin
>    x := x*2;
>    y := y*4;
>    return x+y;
>   end
> 
>   one , two : integer :=1;
>   begin
>     two := change(one,two)+ change(one,two);  -->
> end
> -----------------------------
> 
> They say it will generate
> 
> warning: result may differ if evaluated after actual in expression.
> 
> I understand that the order of calls in the A()+B() is not defined.
> i.e. A() can be called first, then B(), or B() first then A().
> 
> But the result should still be the same either way. So what
> is the problem?

Obviously, it *would* be a problem if you said

    two := change(one,two) - change(one,two);

The language is designed to try to minimize order dependencies that could produce different results.  Asking it to make special cases for commutative operators when both operands are identical expressions, is asking too much.

It looks to me like whoever wrote that slide should have used "-" (or some other function) instead of "+", to avoid any possible confusion in people who realize that the order doesn't matter in this case.

                            -- Adam



  parent reply	other threads:[~2012-06-20 14:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-20  8:24 why this construct causes aliasing problem in 2012 with functions allowing In Out? Nasser M. Abbasi
2012-06-20  8:26 ` Nasser M. Abbasi
2012-06-20 14:56 ` Adam Beneschan [this message]
2012-06-20 15:14   ` Dmitry A. Kazakov
2012-06-21 20:06     ` Randy Brukardt
2012-06-21 20:00   ` Randy Brukardt
replies disabled

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