Replies: 2 comments
-
No, in all the languages I know, there is no way to narrow variable 'b' based on variable 'a'. Although I could implement it, it would significantly affect the performance for other that don't use this approach. I am not willing to greatly reduce performance just for a special preference in Lua. I suggest you also check the err variable; this is reasonable in itself. |
Beta Was this translation helpful? Give feedback.
-
I guess what I am asking is about tuples being actual types, like in Rust where It would formalize types where tuples are used such as return types, I understand it would probably be a big change, but it's an idea. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It's a common pattern to have functions that return a result and an error and have the client code to check if the result is
nil
and assume the function returned an error, like this:and it's also common to see this pattern being used with
assert
:which will type-narrow
res
toT - ?
(non-nil).But the only way I (and several libraries I've seen) to annotate such a function would be:
which means the return type is a combination
(string, string) | (string, nil) | (nil, string) | (nil, nil)
, which is incorrect and will require type narrowing for every possible permutation.using overload doesn't work too:
Is there any way to restrict the return type to
(string, nil) | (nil, string)
and have automatic type narrowing on eitherif not res
orif err
?Beta Was this translation helpful? Give feedback.
All reactions