luash icon indicating copy to clipboard operation
luash copied to clipboard

I think it is a bug

Open Freakwill opened this issue 6 years ago • 1 comments

Let's see the following code. whenever a variable is nil, it will be a function if import sh.

require('sh')
print(type(x), type(nil))  -- function, nil
x = nil
print(type(x))   -- function, but x is set to be nil
t = {}
x = 'hello'
z = t[x]
print(type(t[x]), type(z))  -- nil, function, but z should be nil as usual.

I forgive the first one, since x may be set to be a function by sh, but how to explain next two?

Freakwill avatar Apr 02 '19 12:04 Freakwill

This is the ordinary logic of Lua; setting a variable to nil is the same thing as never setting it in the first place.

So there's no difference between z = nil (and z = t[x] on an empty t is just a fancy way of saying z = nil) and not setting z in the first place. In both cases, the failed lookup on _G triggers the __newindex patch, causing a shell function to be returned.

mnemnion avatar Dec 08 '19 17:12 mnemnion