-
-
Notifications
You must be signed in to change notification settings - Fork 331
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
[Feature Request] Required nullable arguments #2070
Comments
I agree with the general idea. If it is implemented, introducing a special operator like Perhaps another kind of nil can be introduced (e.g. |
What about changing the behavior of existing features? What if: ---@param x integer|nil
local function f(x) end would require for a function to be called as ---@param x integer?
local function f(x) end would allow calling the function as To sum up, |
I'm partial to |
There are cases where I find using On the other hand, I always felt that having three different ways to annotate optional parameters ( So right now all of these functions behave similarly when being called without any arguments: ---@param x integer?
local function f1(x) end
---@param x? integer?
local function f2(x) end
---@param x? integer
local function f3(x) end
---@param x integer|nil
local function f4(x) end
---@param x? integer|nil
local function f5(x) end My idea is that f1 and f4 will now give a warning if called without explicitly passing a parameter. Existing code that relies on putting the question mark after the type will have to be updated, but it seems like a good way to differentiate between the concept of optional arguments and that of nil-able types. Interestingly, the current type information when hovering is identical for all except for f4, which doesn't deduce that Anyway, this is mostly bikeshedding at this point, assuming the feature can be implemented. :) |
I also agree that it's not nice to break previously working annotations, but that's when a major version bump should be made in semantic versioning. |
When calling Java methods from Kahlua, the number of arguments is used to determine the appropriate implementation to call. Because of this, arguments that can be
nil
are required in the function call for appropriate resolution. This is usually not a concern in Lua, but it could be.In this example, both
f()
andf(nil)
are appropriate, since the value ofx
defaults tonil
.In this example, however, the behaviors of
t.f()
andt.f(nil)
are not equivalent.(Admittedly, this is a contrived example. Nonetheless, it shows that this would not only be useful for strange cases like Kahlua's implementation.)
I don't feel strongly about the annotation that would be used to specify this, but
!
comes to mind:The
?!
/!?
pair may be redundant, since non-nil argument types are always required. It would also be feasible to represent required but nil-able arguments with!
alone.The text was updated successfully, but these errors were encountered: