[swift-evolution] [Draft] Tuple-Based Compound Optional Binding

Patrick Smith pgwsmith at gmail.com
Tue Jun 14 04:31:26 CDT 2016


Thanks Xiaodi. Interesting arguments there. It possibly seems a shame to me, because it has knock on effects of making other things more complicated. But I do see how for the most simple case of unwrapping a single Optional, it makes sense.

As much as I would like Brent’s proposal to make things easier to type, I think nesting things inside a tuple, where a reader must keep track of which input matches which output, could lead to harder to follow code.

In my own code I will probably stick to using the long form `guard let a = opt1, let b = opt2, let c = opt3 else { … }`, not because I would like typing it, but because it will be the most clear.

I would have loved for SE-0099 to have gone with semicolon/newline separators so I could write it like this, but alas:

guard
  let a = opt1
  let b = opt2
  let c = opt3
else {
  …
}


A different thought — maybe if Xcode had autocompletion to unwrap Optionals when inside an `if` or `guard` statement it would make things a lot easier. Just type the full name of the optional, then press escape, and then code will be suggested to unwrap it. Is the pain this proposal targets mainly in the writing more than reading?


guard opt·|·


guard let <#placeholder#> = opt1


guard let a = opt1


guard let a = opt1, opt2·|·


guard let a = opt1, let <#placeholder#> = opt2


guard let a = opt1, let b = opt2



Patrick

> On 14 Jun 2016, at 2:07 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
> Also, see: https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md <https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md>
> 
> 
> On Mon, Jun 13, 2016 at 11:06 PM, Xiaodi Wu <xiaodi.wu at gmail.com <mailto:xiaodi.wu at gmail.com>> wrote:
> This has been suggested before, I believe. The core team has weighed in several times; it seemed like there was some disagreement amongst them whether the current syntax is the wisest, but the concluding statement seemed uncontroversial: "I don't think it's something we can change."
> 
> Source:
> http://article.gmane.org/gmane.comp.lang.swift.evolution/15879/ <http://article.gmane.org/gmane.comp.lang.swift.evolution/15879/>
> 
> 
> On Mon, Jun 13, 2016 at 10:32 PM, Patrick Smith via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> If Pyry’s suggestion remained the preferred way of unwrapping a tuple, could it also become the only way for unwrapping a single item?
> 
> guard case let a? = opt1 {
> ...
> }
> 
> Or even shortened for matching optionals only:
> 
> guard let a? = opt1 {
> ...
> }
> 
> Or even as has often been requested, to keep the same name:
> 
> guard let opt1? {
> ...
> }
> 
> Multiples:
> 
> guard let (opt1?, opt2?, opt3?) {
> ...
> }
> 
> guard let (a?, b?, c?) = (opt1, opt2, opt3) {
> ...
> }
> 
> Sorry, not trying to derail, but it always has seemed like something shorter and more self explanatory could be made for optionals. `?` in pattern matching is a special syntax anyway, so why not make this common use case easier?
> 
> Patrick
> 
> _____________________________
> From: Pyry Jahkola via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>
> Sent: Sunday, June 12, 2016 10:04 PM
> Subject: Re: [swift-evolution] [Draft] Tuple-Based Compound Optional Binding
> To: Brent Royal-Gordon <brent at architechies.com <mailto:brent at architechies.com>>
> Cc: swift-evolution List <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>
> 
> 
> 
> 
> On 12 Jun 2016, at 14:46, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> 
> guard let (a, b, c) = (opt1, opt2, opt3) else { ... }
> 
> You mention `guard case` in the motivation, but I think for the uninitiated reader it would be fair to point out that the following example already works equivalently, with only a few extra characters:
> 
> guard case let (a?, b?, c?) = (opt1, opt2, opt3) else { ... }
> 
> Aside of that, it's yet more magic to our `if let` syntax but I don't mind, it would be useful at times.
> 
> — Pyry
> 
> 
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160614/649bb530/attachment.html>


More information about the swift-evolution mailing list