Explanation about decltype used in typechecker_spec.lua #88
-
In the following lines nelua-lang/spec/typechecker_spec.lua Lines 1814 to 1816 in e6727dd local x: auto = 1 and then local y: decltype(x) = x Does this mean |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The
Not exactly, Nelua's Here is a small example of decltype usage: local function bounded_increment(x: auto): auto
local y: decltype(x) = x + 1
if y <= x then -- value wrapped around due to integer overflow
y = x
end
return y
end
print(bounded_increment(0.0)) -- outputs: 1.0
print(bounded_increment(4294967295_u32)) -- outputs: 4294967295 So |
Beta Was this translation helpful? Give feedback.
The
decltype
in Nelua is a generic that returns the type of an identifier or expression.Not exactly, Nelua's
decltype
is similar to C++'sdecltype
because you can use both to get a type from an identifier or expression, butdecltype(auto)
in C++ is used only to automatic detect the type for function returns while in Nelua the same is done just withauto
or leaving the function returns without type notations.Here is a small example of decltype usage: