-
Notifications
You must be signed in to change notification settings - Fork 99
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
check return value and argument number #100
Comments
@virusdefender Thanks for the report! This is a tricky one. This is very much related to #71, except that that one is about input arguments to a function, and this one is about output values. The reason why Teal (currently) accepts missing return values is because any return value may be It is a very common pattern than functions in Lua will return sometimes one, sometimes two values: function f(x)
local result = x * 2
if x < 10 then
return nil, "oops"
end
return result
end The "real" result type of How to destructure this type on the caller gets pretty tricky. Common Lua pattern: local x, y = f()
if not x then
print("error: " .. y)
end The above looks logical (if x is nil, then y is string), but it has a bunch of things to consider when making the compiler smart enough to understand this:
It goes on and on... Putting all that aside, the simplest strict thing would be to force an explicit arity on the return values. So, if the return is This is the simplest to implement, but I think it would be pretty annoying to use, because considering error results of the type Another option, would be to do the same as what is considered on #71, and add notation not about nullability, but about explicit arity (that is, the number of return expressions, regardless of their values and types). One issue with that is syntactical: how to specify it? In #71 the function f(x: number): number, ?string
local result = x * 2
if x < 10 then
return nil, "oops"
end
return result
end ...which is pretty ugly. I did not pick the Sorry for the braindump here, but I thought it was useful to write these thoughts down! I'll give this some more thought and possibly experiment with this a little. |
Thank you for the quick reply
This is necessary for code robustness, and it's also a common pattern in golang https://blog.golang.org/error-handling-and-go . BTW, there is no optional argument in golang, it's not a problem. Under my scenario, i want to force programmers to check errors like golang to avoid bugs, such as file operation, or there will nobody to check if the operation is successful, and then the pass the return value (may be a nil) to the next block and then cause some bugs. The reason i chose teal-lang is that it can help to to deal with above issues, using lua like a static language, i think the goal of teal-lang is not only to provide types, but also help to write more safe and robust code. Maybe teal-lang can provide a more strict mode to check code? At the first stage, the hard flow analysis can be skipped, the goal is to check the direct return statement variable number. local function a(i: number): number, number
if i == 1 then
return 1, 2
else if i ==2 then
-- it's easy to check, i think
return 3
else
-- need flow analysis?
end
end |
That's an interesting idea. The stricter check of arity could be a warning flag. |
for example
some related code
https://github.com/teal-language/tl/blob/master/spec/return/arity_spec.lua#L38 it should be
rejects in strict
?tl/tl.tl
Line 5401 in 8b3c19a
master
is a moving target]The text was updated successfully, but these errors were encountered: