- Proposal: TBD
- Author: Brent Royal-Gordon
- Status: TBD
- Review manager: TBD
This proposal introduces tuple-based if-let assignments to the Swift programming language.
Swift Evolution Discussion: [Accepted with Revision] SE-0099 Restructuring Condition Clauses
SE-0099 removed cascaded if-let statements that allowed groups of conditional binding to omit the let
keyword. This proposal re-introduces the convenience of multiple simultaneous conditional assignments.
Instead of using cascades like:
guard let a = opt1, b = opt2, c = opt3 else { ... leave scope ... }
You must now use the let
keyword for each binding:
guard let a = opt1, let b = opt2, let c = opt3 else { ... leave scope ... }
Given that you can still use tuples with case
, e.g.
guard case (.none, .none, .none) = (foo(), bar(), blort()) else { ... }
guard case (.Some(let a), .Some(let b), .Some(let c)) = (foo(), bar(), blort()) else { ... }
It seems only fair that if-let
, guard-let
, and while let
should permit an analogous construct without using the extra verbiage required for case
:
guard let (x, y, z) = (foo(), bar(), blort()) else { ... } // illegal
Unfortunately, they do not:
// error: initializer for conditional binding must have
// Optional type, not '(Int?, Int?, Int?)' (aka
// '(Optional<Int>, Optional<Int>, Optional<Int>)')
This proposal extends Swift's public facing API to support conditional tuple bindings with simple let
patterns, just as it does for case
:
optional_binding_condition : 'let' pattern initializer | 'var' pattern initializer
This proposal is additive
Not accepting this proposal