Aliasing [was Re: [Tutor] beginning to code]

Antoon Pardon antoon.pardon at rece.vub.ac.be
Tue Sep 26 12:26:06 EDT 2017


On 26-09-17 14:28, Steve D'Aprano wrote:
> On Tue, 26 Sep 2017 03:26 am, Antoon Pardon wrote:
> 
>> Sorry, what he wrote contradicts that. Maybe he was just really confused
>> at that moment, but it still needed correction. If the assignment is
>> an alias operator then after the statements
>>
>> a = 1
>> b = a
>> b = 2
>>
>> a is not 2.
> 
> How do you get that conclusion? I think you are mistaken.
> 
> If assignment were an alias operation, then a would be 2, because b is an alias
> to a. That's how var parameters in Pascal work, and out parameters in Ada, and
> both are described as aliasing.

They work like that because the assignment in Pascal is *not* an alias operation.

> Its also how reference variables ("aliases") in C++ work.
> 
> https://www.tutorialspoint.com/cplusplus/cpp_references.htm
> 
> https://stackoverflow.com/a/17025902
> 
> 
> Here's some C++ code that demonstrates it. Apologies in advance if it isn't the
> most idiomatic C++ code.

No that C++ code doesn't demonstrate your assertion. You ignore the fact that
only the 7th line is an alias operation, all the assignments (later) are copy
operations.

> #include <iostream>
> #include<stdio.h>
> using namespace std;
> 
> int main () {
>    int a;
>    int& b = a;  // reference variable or alias
> 
>    a = 1;
>    printf("a: %d, alias b: %d\n", a, b);
>    b = 2;
>    printf("a: %d, alias b: %d\n", a, b);
>    return 0;
> }

Because similarity of notation may be causing some confusion, I'll introduce two
new symbols.

:= This is the symbol I use for an assignment as copy operation, like Pascal.
<- This is the symbol I use for an assignment as alias operation.

So if the assignment is an alias operation, the code I was making an assertion
about, rewritten using these symbols would be:

a <- 1
b <- a
b <- 2

The code you use in support of your assertion instead behaves more like.

b <- a   # The order of the first two statements
a := 1   # doesn't matter, my criticism doesn't depend on that.
b := 2

If you want to make a statement about the effects of assignemt as an
alias operation, you will have to support that with code that actually
behaves like an alias operation each time an assignment is made. Not
code like your C++ example which only executes one alias operation and
where the rest of the assignments are copy operations.

-- 
Antoon Pardon.



More information about the Python-list mailing list