Replies: 1 comment
-
As far as I know the only other way to infer the type is to explicitly pass its name as a parameter (backtick capture) ---@generic T : string|integer
---@param t `T`
---@param p1 string
---@return T
local function Get(t, p1)
if (t == "string") then
return "1"
end
return 2
end
local val = Get('string', 'key') -- val is string
tester(Get('number', 'key')) -- warning: cannot assign number to string parameter which is a bit like the C# example but with less type safety. Speaking of generics, I'm not sure what the significance of the parent type ( |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
My background is in C# development. When I create a method that could return back one of several different types, I can explicitly specify that type as a part of the call (e.g.,
var value = Test.Get<string>("key");
and the IDE will understandvalue
is typed asstring
.I've read over the
@generic
doco in the wiki. I'm trying to understand what's going on here. Given:If I have code to call tester
neither of these gives me a warning. This is probably ok for the first call, but why is it ok for the second?
Is there a mechanism (other than passing a parameter into a function that is typed
T
) of specifying the output type in this scenario? I've found some info to suggest type inference is possible, but when I do something like:I see
Beta Was this translation helpful? Give feedback.
All reactions