[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: function aa(a,a) return a end -> bug ?
- From: Jonathan Goble <jcgoble3@...>
- Date: Tue, 27 Nov 2018 14:07:47 -0500
I just found when reading
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
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 ?
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)
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.