-
Notifications
You must be signed in to change notification settings - Fork 42
'local'-keyword has unexpected behavior #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This should be partially addressed by commit 28bae65. Currently, only specific cases of assignments are transpiled into the expected one-liner form. For example, the following cases are handled correctly: -- supported cases
local x = 1
local y, z = "sds", false
local n, m = func! Which transpile to: local x = 1
local y, z = "sds", false
local n, m = func() However, there are still cases where this behavior is not yet supported, such as: -- unsupported cases
local a = tb?\func!
local b = item\end 123
local c = if true then 1 These are transpiled into the following Lua code: local a
do
local _obj_0 = tb
if _obj_0 ~= nil then
a = _obj_0:func()
end
end
local b
local _call_0 = item
b = _call_0["end"](_call_0, 123)
local c
if true then
c = 1
end In these unsupported cases, the variable declaration and assignment are still split due to the complexity of the expressions. |
This is a sensible solution. However, for the sake of consistency, how about doing the following: Given this YueScript code: local x = if x then "Yes" else "No" Generate this: local x = x
if x then
x = "Yes"
else
x = "No"
end The compiler could observe if the expression that's being assigned to |
Still not clear why this is unsupported, and why it ever needs extra _call_0 variable. |
In this code, the compiler interprets So that the following code should be handled as one-liner, while it is not yet done. item = {}
local b = item\end 123 |
@SkyyySi -- You can try the code in YueScript 0.27.1
_ENV = <index>: (key) => print "accessing a global named #{key}"
local x = x |
But if your expression explicitly asks to accesses a variable
I don't think either of those are compelling arguments. There are also genuine reasons why someone might want this to happen. Primarily, that would be caching global variables. It's a common thing in Lua to write something like this: local assert = assert
local pcall = pcall
local string = string
--- You get the idea Besides that: If someone actually does something as horrible as triggering side-effects whenever the global environment is accessed, then they deserve whatever happens to them ;) |
I think the argument is that you wouldn't expect the side-effect twice when you only see it once in your code. YueScript caches it so the side effect is only executed once. |
|
I know how assignments work. I'm talking about a case where the variable gets cached, like the case posted earlier in this very thread: local b
local _call_0 = item
b = _call_0["end"](_call_0, 123) As long as it's compiled this way, it has to cache |
In those case, falling back to an IIFE is always an option: local b = ((function(_call_0)
return _call_0["end"](_call_0, 123)
end)(item)) Or, alternatively, an additional temporary variable can be generated by the compiler: local _b_temp_0
local _call_0 = item
_b_temp_0 = _call_0["end"](_call_0, 123)
local b = _b_temp_0 Both would preserve the intended behaviour with function environments. |
Uh oh!
There was an error while loading. Please reload this page.
Currently,
local
behaves in an unexpected way. For example, this YueScript code ...... compiles to the following Lua code:
I would appreciate it if this was changed to behave the same as it does in Lua. If someone is going out of their way to explicitly declare the variable in the same statement, then there's likely a reason for it. If someone really wanted to pre-declare the variable, they could just do that instead.
The text was updated successfully, but these errors were encountered: