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
In Swift's current incarnation, annotating methods and functions with `@warn_unused_result` informs the compiler that a non-void return type *should be consumed*. It is an affirmative declaration. In its absence, ignored results do not raise warnings or errors.
11
10
12
11
In its present form, this declaration attribute primarily differentiate between mutating and non-mutating pairs. It offers an optional `mutable_variant` for when an expected return value is not consumed. For example, when `sort` is called with an unused result, the compiler suggests using `sortInPlace` for unused results.
@@ -54,7 +53,7 @@ In the four examples mentioned here, this proposal makes the following recommend
54
53
* Collections example: This could be argued either way. In such a situation, should be left unmarked. Leaving the implementation unmarked encourages an act from the consumer to either handle the result or actively dimiss the warning.
55
54
*`printf` example: importing C calls is left as an exercise for the Swift team
56
55
57
-
*Note: This proposal does not ignore the positive utility of pairing in-place/procedural and value-returning/functional implementations.and retains the ability to enable Xcode system to cross reference between the two.*
56
+
*Note: This proposal does not ignore the positive utility of pairing in-place/procedural and value-returning/functional implementationsand retains the ability to enable Xcode system to cross reference between the two. We propose that the declaration attribute surrenders that responsibility and passes it to the rich documentation comment system.*
58
57
59
58
## Detail Design
60
59
@@ -64,31 +63,25 @@ Under this proposal, the Swift compiler emits a warning when any method or funct
64
63
_=discardableResult()
65
64
```
66
65
67
-
While this workaround makes it clear that the consumption of the result is intentionally discarded, it offers no traceable intent as to whether the API designer meant for this use to be valid. Including an explicite attribute ensures the discardable return value use is one that has been considered and approved by the API author.
66
+
While this workaround makes it clear that the consumption of the result is intentionally discarded, it offers no traceable intent as to whether the API designer meant for this use to be valid. Including an explicit attribute ensures the discardable return value use is one that has been considered and approved by the API author.
68
67
69
68
The approach takes the following form:
70
69
71
70
```swift
72
-
funcf() -> T {} //defaults to warn on unused result
73
-
funcg() ->@discardable T {} // may be called as a procedure as g()
74
-
//without emitting a compiler warning
71
+
@discardableResultfuncf() -> T {} //may be called as a procedure as g()
72
+
// without emitting a compiler warning
73
+
funcg() -> T {} //defaults to warn on unused result
75
74
funch() {} // Void return type, does not fall under the umbrella of this proposal
76
75
```
77
76
78
-
Decorating the return type makes it clear that it's the result that can be optionally treated as discardable rather than the function whose role it is to police its use.
79
-
80
-
`@discardable T` offers a covariant specialization of `T` and Void, enabling it to satisfy non-discardable requirements:
77
+
The following examples demonstrate the `@discardableResult` behavior:
81
78
82
79
```swift
83
-
funcf() ->@discardable T {}
84
-
funcg() -> T {}
85
-
funch() {} // h() -> Void
86
-
87
-
let c1: () -> T = f // no compiler warning
88
-
let c2: () ->Void= f // no compiler warning
89
-
let c3: () ->@discardable T = f // no compiler warning
90
-
let c4: () ->@discardable T = g // compiler warning about incompatible types
91
-
let c5: () ->@discardable T = h // compiler warning about incompatible types
80
+
let c1: () -> T = f // no compiler warning, OK
81
+
let c2: () ->Void= f // compiler error, not allowed
82
+
let c3 = f // assignment does not preserve @discardableResult attribute
83
+
c3() // warning unused result
84
+
_=c3() // no compiler warning, OK
92
85
```
93
86
94
87
### Mutating Variants
@@ -102,19 +95,24 @@ This proposal recommends introducing two further comment fields, specifically `m
102
95
103
96
The `message` argument that formerly provided a textual warning when a function or method was called with an unused result will be discarded entirely and subsumed by document comments. Under this scheme, whatever attribute name is chosen to modify function names or return types will not use arguments.
104
97
105
-
## Conventional Alternatives Considered
98
+
## Alternative Names Considered
106
99
107
-
Other keywords considered for decorating the type included: `@ignorable`, `@incidental`, `@elective`, `@discretionary`, `@voluntary`, `@voidable`, `@throwaway`, and `@_` (underscore).
100
+
Alternative names considered included names were considered: `@allowUnusedResult`, `@optionalResult`, `@suppressUnusedResultWarning`, `@noWarnUnusedResult`, `@ignorableResult`, `@incidentalResult`, and `@discretionaryResult`.
108
101
109
-
Our alternative approach takes a prefix form, marking the declaration with an attribute:
102
+
## Future directions
103
+
104
+
The Swift Evolution community also discussed decorating the type rather than the declaration.
105
+
Decorating the return type makes it clear that it's the result that can be optionally treated as discardable rather than the function whose role it is to police its use.
110
106
111
107
```swift
112
-
@attributefuncf() -> T {}
108
+
funcf() ->@discardable T {} // may be called as a procedure as f()
109
+
// without emitting a compiler warning
113
110
```
114
111
115
-
The attribute retains the placement of `@warn_unused_result`, for example `@allowUnusedResult`.
112
+
This approach was discarded to reduce the type system impact and complexity of the proposal. When not coordinated with the base function type, currying or "taking the address" of a function could effectively remove the @discardableResult attribute. This means some use of an otherwise `@discardable` function value would have to use `_ =`. While this approach was considered more elegant, the additional implementation costs means that it's best to delay adopting type decoration
113
+
until such time as there's a strong motivation to use such an approach.
116
114
117
-
For prefix attributes, the following names were considered: `@allowUnusedResult`, `@optionalResult`, `@suppressUnusedResultWarning`, `@discardableResult`, `@noWarnUnusedResult`, `@ignorableResult`, `@incidentalResult`, and `@discretionaryResult`.
115
+
Keywords considered for decorating the type included: `@discardable`, `@ignorable`, `@incidental`, `@elective`, `@discretionary`, `@voluntary`, `@voidable`, `@throwaway`, and `@_` (underscore).
118
116
119
117
## Unconventional Alternatives
120
118
@@ -143,10 +141,10 @@ surmounted, this would offer a simple, elegant solution.
143
141
144
142
## Snake Case
145
143
146
-
It should be noted that this proposal, if accepted, removes two of the last remaining instances of snake_case in the Swift language. This further brings the language into a coherent and universal use of lowercase and camelcase variants.
144
+
It should be noted that this proposal, if accepted, removes two of the last remaining instances of snake_case in the Swift language. This further brings the language into a coherent and universal use of lowercase and camel case variants.
147
145
148
146
## Acknowledgements
149
147
150
148
Changing the behavior of non-void functions to use default warnings for unused results was initially introduced by Adrian Kashivskyy. Additional thanks go out to Chris Lattner, Gwendal Roué, Dmitri Gribenko, Jeff Kelley, David Owens,
151
149
Stephen Cellis, Ankit Aggarwal, Paul Ossenbruggen,
0 commit comments