-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Idea: Better handling of templates in CreateFrame? #121
Comments
Well, I guess the static definition is not enough if a non-base type inherits from a template, so what would really be needed is a dynamic class definition based on the generic parameters :( |
I can confirm these steps fix the Dynamic class definitions would be cool, something like LuaLS/lua-language-server#1861 |
I think the "proper" solution would be a luals plugin that transforms CreateFrame calls and adds |
proof of concept plugin, works for trivial cases; unfortunately LuaLS only allows for text -> text transformations, having the syntax tree available would be much better. And given how fragile this is I don't think anyone should use it, just playing around with plugins here.
This correctly handles cases like |
FWIW LuaLS added AST transformations for plugins recently and I built a plugin for Deadly Boss Mods which has a somewhat similar problem, maybe this can be an inspiration on how to resolve the template problem in a somewhat neat way: https://github.com/DeadlyBossMods/LuaLS-Config/blob/main/DBM-Plugin.lua |
Thanks, I will surely look into your luals plugin example. I have no experience with the AST, but it looks interesting. |
I have code like this:
The definition for CreateFrame makes Frame of type
BackdropTemplate|Frame
which means it triggersparam-type-mismatch
onframe:ApplyBackdrop()
becauseBackdropTemplate|Frame
!=BackdropTemplate
. I also can't cast frame to the template type because it doesn't inherit from Frame, so it would then fail onframe:Show()
instead.Suggestion for a fix in two steps:
Frame
. Given that only BackdropTemplate exists this is just:---@class BackdropTemplate: Frame
Now you can cast frame to BackdropTemplate or add an @type annotation :)
CreateFrame
would already return the correct type, but I don't think "return type is arg4 if it exists, arg1 otherwise" can be fully modeled in the type system.However, there is a work-around, we can duplicate the definition with different parameter numbers!
It's a bit ugly because template isn't the last parameter, but no one uses
id
anyways I think.The class definition together with the duplicate CreateFrame make my example work correctly without any annotations :)
The text was updated successfully, but these errors were encountered: