Discussion:
function aa(a,a) return a end -> bug ?
Domingo Alvarez Duarte
2018-11-27 18:53:11 UTC
Permalink
Hello !

I just found when reading
http://www.cse.chalmers.se/edu/year/2012/course/DAT150/lectures/plt-book.pdf
on page 80 see bellow:

===

To be really picky, the type checker of function definitions should also
check that all variables in the parameter list are distinct. We shall
see in the
next section that variables introduced in declarations are checked to be
new.
Then they must also be new with respect to the function parameters.

===

I decided to check lua and found that lua/luajit accept this without
complain:

===

function aa(a,a) return a end

print(aa(1,2))  --> 2

===

Is this the expected behavior or a bug ?

Cheers !
Jonathan Goble
2018-11-27 19:07:47 UTC
Permalink
Post by Domingo Alvarez Duarte
Hello !
I just found when reading
http://www.cse.chalmers.se/edu/year/2012/course/DAT150/lectures/plt-book.pdf
===
To be really picky, the type checker of function definitions should also
check that all variables in the parameter list are distinct. We shall
see in the
next section that variables introduced in declarations are checked to be
new.
Then they must also be new with respect to the function parameters.
===
I decided to check lua and found that lua/luajit accept this without
===
function aa(a,a) return a end
print(aa(1,2)) --> 2
===
Is this the expected behavior or a bug ?
Cheers !
Feature, I'd say. Consider the example of a game plugin, when the game
always passes four arguments, but your function only cares about the last
two:

function onclick(_, _, x, y)
drawDot(x, y)
end

A variable name of "_" is usually understood to mean "I don't care about
this value". Some static analysis tools in some languages will suppress
certain warnings like unused variables when the variable of concern is
named "_", so using that twice signals your intent to both humans and
analysis tools. Another somewhat similar case is multiple return values
where you only care about the last one.

That said, if you actually depend on which value the duplicated variable
gets, that's a major code smell at best and undefined behavior at worst. So
you should only do this for values that you don't care about at all.
Thijs Schreijer
2018-11-27 21:10:05 UTC
Permalink
Post by Domingo Alvarez Duarte
Hello !
===
To be really picky, the type checker of function definitions should also
check that all variables in the parameter list are distinct. We shall see in the
next section that variables introduced in declarations are checked to be new.
Then they must also be new with respect to the function parameters.
===
===
function aa(a,a) return a end
print(aa(1,2)) --> 2
===
Is this the expected behavior or a bug ?
Cheers !
Expected imo, you create 2 locals called a, the second one shadowing the first (assignments go left to right), so when you return, you return the second one.

Thijs
Philippe Verdy
2018-11-28 12:52:37 UTC
Permalink
It.s not clear from the doc about which variable is created the first and
which one will shadow the other ones made unreachable in scope.

The same could be said about the declaration in a single statement:

local a,a=1,2

Or the single assignment statement of a list:

a,a = fun()

or:

a,a = ...

I think we can expect the assignments of variabkes being performed from the
leftmost variable to the rightmost one which wins.
Post by Domingo Alvarez Duarte
Post by Domingo Alvarez Duarte
Hello !
I just found when reading
http://www.cse.chalmers.se/edu/year/2012/course/DAT150/lectures/plt-book.pdf
Post by Domingo Alvarez Duarte
===
To be really picky, the type checker of function definitions should also
check that all variables in the parameter list are distinct. We shall
see in the
Post by Domingo Alvarez Duarte
next section that variables introduced in declarations are checked to be
new.
Post by Domingo Alvarez Duarte
Then they must also be new with respect to the function parameters.
===
I decided to check lua and found that lua/luajit accept this without
===
function aa(a,a) return a end
print(aa(1,2)) --> 2
===
Is this the expected behavior or a bug ?
Cheers !
Expected imo, you create 2 locals called a, the second one shadowing the
first (assignments go left to right), so when you return, you return the
second one.
Thijs
Roberto Ierusalimschy
2018-11-28 15:49:26 UTC
Permalink
Post by Domingo Alvarez Duarte
To be really picky, the type checker of function definitions should also
check that all variables in the parameter list are distinct. [...]
Lua is not that picky :-)

-- Roberto

Continue reading on narkive:
Loading...