You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now the type of h is () -> Int, but this might come unexpected, because you use g at other places while ignoring its result. Also, even if you are aware that g has a discardable result, the type of h is now intuitively unclear.
Proposed solution
If a discardable result determines the type of a closure as described in the example in the motivation part, there should be a warning. A warning should not hurt existing code bases too much, as code then still compiles if it compiled before. In the example above, you can add “return ” or “_ = “ to silence that warning:
leth={()inreturng()} // h: () -> Int
or
leth={()in _ =g()} // h: () -> ()
A warning is not necessary in the following cases:
leth={()inif myCondition {2}else{g()}} // h: () -> Int
There are cases where type annotations (and to make them possible maybe some further refactoring, or some refactoring alone without added type annotations) are then necessary to silence the warning:
The alternative is to leave it as it is (no warning).
Additional information
The issue came up in the discussion of the Swift forums topic Last expression as return value. The pitch discussed there would be source breaking in a case that would get the warning proposed here.
The text was updated successfully, but these errors were encountered:
Motivation
Consider the code
Clearly,
h
is of type() -> ()
.But you might want to call the following function
g
instead off
inh
, becauseg
does something more appropriate:Now the type of
h
is() -> Int
, but this might come unexpected, because you useg
at other places while ignoring its result. Also, even if you are aware thatg
has a discardable result, the type ofh
is now intuitively unclear.Proposed solution
If a discardable result determines the type of a closure as described in the example in the motivation part, there should be a warning. A warning should not hurt existing code bases too much, as code then still compiles if it compiled before. In the example above, you can add “return ” or “_ = “ to silence that warning:
or
A warning is not necessary in the following cases:
and
There are cases where type annotations (and to make them possible maybe some further refactoring, or some refactoring alone without added type annotations) are then necessary to silence the warning:
With another function with a discardable result
you would have to write
or
Alternatives considered
The alternative is to leave it as it is (no warning).
Additional information
The issue came up in the discussion of the Swift forums topic Last expression as return value. The pitch discussed there would be source breaking in a case that would get the warning proposed here.
The text was updated successfully, but these errors were encountered: