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
Wrong return type for file:read #81
Comments
Ideally we'd be able to annotate |
Isn't The issue here is that there is no type annotation at all for |
Yes I suppose using generics that would work here, although I swear I had a similar issue in a project of mine just a few days ago, hmm. Anyway, indeed annotations are missing for file:read |
I still need to write tests, but seems as this is being actively discussed. I'm currently looking at typing 5.0-5.1 ---@overload fun(): nil | string
---@overload fun(format: "*a"): string
---@overload fun(...: "*n"): (nil | number), (nil | number)...
---@overload fun(...: "*l"): (nil | string), (nil | string)...
---@overload fun(...: number): (nil | string), (nil | string)...
---@vararg "*a" | "*n" | "*l" | number
---@return nil | string | number, (nil | string | number)... 5.2 ---@overload fun(): nil | string
---@overload fun(format: "*a"): string
---@overload fun(...: "*n"): (nil | number), (nil | number)...
---@overload fun(...: "*l"): (nil | string), (nil | string)...
---@overload fun(...: "*L"): (nil | string), (nil | string)...
---@overload fun(...: number): (nil | string), (nil | string)...
---@vararg "*a" | "*n" | "*l" | "*L" | number
---@return nil | string | number, (nil | string | number)... 5.3-5.4 ---@overload fun(): nil | string
---@overload fun(format: "a" | "*a"): string
---@overload fun(...: "n" | "*n"): (nil | number), (nil | number)...
---@overload fun(...: "l" | "*l"): (nil | string), (nil | string)...
---@overload fun(...: "L" | "*L"): (nil | string), (nil | string)...
---@overload fun(...: number): (nil | string), (nil | string)...
---@vararg "n" | "a" | "l" | "L" | number | "*a" | "*n" | "*l" | "*L"
---@return nil | string | number, (nil | string | number)... Basically, at present it's impossible to properly type this function. We'd need some pretty elaborate type transform logic to make it work 100% reliably i.e. the nth output type is dependent on the nth input type. However, I believe the above should cover off the most common use cases. |
Wow, that already looks impressively accurate, given that the specification of |
Luanalysis v1.2.3
What happens?
What were you expecting to happen?
No error message since
f:read("a")
returns a string.This is related to #80, where the return type depends on the string value of the first argument as well. Since the type system allows string values as separate types, this one should be solvable with a few overrides though, at least for the most common formats.
The text was updated successfully, but these errors were encountered: