[swift-evolution] Throws, rethrows and declaration-modifiers in function declarations
James Froggatt
james.froggatt at me.com
Tue Jul 19 15:38:33 CDT 2016
Closures can throw too. The ability to throw is part of the function type. Keywords preceding ‘func’ relate to the method, as a ‘container’ entity for the function, rather than the function itself.
I agree the syntax is odd, the keyword should probably come after the arrow.
Relatedly, what happened to the follow up proposal to change @discardableResult to an @discardable annotation on the return type? As it is, closure properties can't have discardable results.
------------ Begin Message ------------
Group: gmane.comp.lang.swift.evolution
MsgID: <1468950690.1969779.670846257.58EFBDA2 at webmail.messagingengine.com>
Hello,
I'm currently reading the updated Swift 3 preview book, and was
wondering about the `throws` and `rethrows` keywords.
In a function declaration, all the keywords are at the beginning, except
for `throws` and `rethrows`, which are⦠oddly placed.
In the grammar:
```
*function-declaration *â *function-head[1] Âfunction-name[2]Â **generic-parameter-clause[3]Â*optÂ*function-
signature[4]Â **function-body[5]Â*optÂ
*function-head *â *attributes[6]Â*optÂ*declaration-
modifiers[7]Â*optÂfuncÂ
*function-signature *â *parameter-clause[8]Â*throwsÂoptÂ*function-
result[9]Â*optÂ
*function-signature *â *parameter-clause[10]Â*rethrowsÂ*function-
result[11]Â*optÂ
```
Is there a reason to have throws and rethrows at this specific position,
instead of declaration-modifiers?
ie.:
This:
```
func send() throws -> String {}
```
Becomes:
```
throwing func send() -> String {}
rethrowing func send() -> String {}
```
Like:
```
mutating func send() -> String {}
```
Or:
```
throw func send() -> String {}
rethrow func send() -> String {}
```
Like:
```
override func send() -> String {}
```
(You can also see the weird difference between `override` and
`mutating`, one using the -ing suffix, the other being infinitive)
Am I missing something? I find that these keywords break the flow of the
function declaration, separating the parameters and the return type,
which is never done anywhere else.
Thanks.
--
Matthieu Oger
Pixelnest Studio[12]
Â
Links:
1. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-head
2. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-name
3. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause
4. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-signature
5. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-body
6. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes
7. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-modifiers
8. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause
9. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-result
10. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause
11. https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-result
12. http://pixelnest.io/
------------- End Message -------------
From James F
More information about the swift-evolution
mailing list