the code in function can't run #2499
-
the code in function can't run well: function Get(type)
return type
end
a = Get(_G) ---can't get `_G`
b = {first=1}
function Set(name)
rawset(b,name,"test")
end
Set("test") ---can't find `test` in `b` i've tried running the above code on other devices and can't achieve the desired results, have used the latest v3.7.4 |
Beta Was this translation helpful? Give feedback.
Answered by
carsakiller
Jan 29, 2024
Replies: 1 comment 2 replies
-
For the first case, we can use generics to return the correct type: ---@generic T
---@param type T
---@return T
function Get(type)
return type
end
a = Get(_G) For the second case, that kind of mutation can't really be annotated at the function, so we would have to ---@param name string
function Set(name)
rawset(b,name,"test")
end
Set("test")
---@cast b {first: 1, name: string} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
KeilAwk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the first case, we can use generics to return the correct type:
For the second case, that kind of mutation can't really be annotated at the function, so we would have to
@cast
: