-
-
Notifications
You must be signed in to change notification settings - Fork 351
Type cast and type narrow proposal #1030
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
string.len(x) ---@as x string ---@as x string
string.len(x) local x
if type(x) == 'string' then
---@as x string # x is `string` in *this block*
-- auto narrow in the future
print(string.len(x))
end |
In a similar vein w/ fesily's example: are guards outside the scope of this proposal/too much semantically? Basic example case: ---@param value unknown
---@return value is string
function is_string(value)
return typeof(value) == "string"
end
---@type unknown
local value
if is_string(value) then
print(value.len(x))
end
Alternatively, more inline w/ proposal syntax (possible conflict with multiple return syntax, however I don't think ---@param value unknown
---@return value +string
function is_string(value)
return typeof(value) == "string"
end
|
I think it should be something like: ---@generic V
---@param value V
---@return V is string
function is_string(value)
return typeof(value) == "string"
end But it is hard to implement |
Version 3 has been implemented. Please open new issues if there are other problems |
Version 1 (deprecated)
Due to Lua's syntax, using inline comments can be verbose and ugly.
Is there any other method based on line comments?
EDIT:
---@narrow
will affect all the block and must declare at the top of blockVersion 2
The text was updated successfully, but these errors were encountered: