-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Unexpected type mismatch when assigning a generic function. #84
Comments
Generic parameter types are only accessible within the scope in which they're defined. The two different I'm not aware of any languages that allow this sort of semantic as assignment typically deals with concrete (runtime) types, and generics are of course abstract ("compile" time / static) types. Can you give an example where this pattern might be useful? |
A hard to workaround situation is when forward declarations are needed for circular references. Like 2 functions calling each other:
Even when it's not needed for circular references, I like to systematically forward-declare all file-scope functions at the beginning of a file. I find it extremely convenient (and less bug prone) to be able to implement the body of a function anywhere in a file, without having to deal with |
Typing the forward declaration is correct, however the duplicate type definitions that follow are unnecessary. Having duplicate type definitions will create a fairly significant maintenance burden, particularly since you're unlikely to be able to take advantage of any refactoring tools. The second set of type definitions belong to the anonymous function, not I think perhaps what you're after is the ability to (forward) declare a variable type using the (multi-line) function doc syntax. That way you'd be able to include function and parameter descriptions. This would also solve another problem we have at present where you can't define a variable as having overloads. EDIT: In saying that there's currently no means to provide descriptions for |
Looking at your first 2 paragraphs, the current supported solution, in terms of type safety, would be: ---@type fun<T>(array:T, index:number)
local fun1
fun1 = function(array, index)
-- Needs to call fun2
end So I would have to get rid of the parameters/returns documentation, which isn't satisfying.
If you're proposing to be supporting something like this: --- This is fun1 documentation.
--- @generic T
--- @param array T[] @Arg documentation
--- @param index number @Index documentation
--- @return T[] @Result documentation
local fun1 = function(array,index) end -- <--- no complain from lack of body
fun1 = function(array, index)
-- Inferred types:
-- array: T[]
-- index: number
return {} -- T[]
end I could indeed easily workaround the bug. |
Correct.
Whilst I agree that they ought to be supported (for forward declarations). As mentioned, they are reasonably useless how you're presently using them, as the documentation belongs to the anonymous functions, and thus is only visible within the function scope. That's not to say regular documentation is pointless, it's just using the type doc annotation syntax doesn't provide much value in this context.
Feature request, not bug. However, I'm proposing: --- This is fun1 documentation.
--- @generic T
--- @param array T[] @Arg documentation
--- @param index number @Index documentation
--- @return T[] @Result documentation
local fun1
fun1 = function(array, index)
-- Inferred types:
-- array: T[]
-- index: number
return {} -- T[]
end i.e. no superfluous function body required. |
Thanks for the fast answers. For now I'll use the @type annotation, and type the documentation without @ param/@ return/@ generic. This way I can get type safety and still decent documentation. Is there a way to refer to
|
Unfortunately not. I'd like to add something like For now the only way to get to that type would be through absurd hacks like: ---@type fun<T>(v:T[])
local fun1
---@type fun<T>(arr: T[]): nil | T
local nilElementType = function(arr) return nil end
fun1 = function(v)
local r = nilElementType(v)
end Yeah... maybe if you're desperate 🤷♂️ |
+1 for |
I resolved the original issue, assigning a generic function to a compatible generic function is now possible. So forward declarations can be used as originally desired. However, there's still the issue with forward declarations not being able to be documented. I've opened #103 to track function type definition limitations. |
Environment
What are the steps to reproduce this issue?
Create a Lua file with the following code:
What happens?
Luanalysis generates a "Type mismatch" error on line
local x = foobar
:What were you expecting to happen?
No error: this should be a valid assign.
Any logs, error output, etc?
None
The text was updated successfully, but these errors were encountered: