[swift-evolution] New feature request: syntactic sugar for "if let" scoped self assignment

Jacob Bandes-Storch jtbandes at gmail.com
Sun Aug 28 21:41:27 CDT 2016


For context, some prior discussions on this topic:

"Reconsidering SE-0003 Removing var from Function Parameters and Pattern
Matching"
(couldn't find one representative message — search it yourself
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/thread.html
)

The bind thread
<https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008649.html>



On Sun, Aug 28, 2016 at 12:54 PM, Nicholas Exner via swift-evolution <
swift-evolution at swift.org> wrote:

> Dear Swift Evolution Group Members,
>          Recently, as I was reviewing some code that I wrote, I was
> wondering what the community’s thoughts are on the possible value of having
> a special “if let” self-assignment operator added to the language (such as
> =? ).  A sort of syntactic sugar that allows for optional unwrapping into a
> scoped variable of the same name as the variable in the larger scope.  Here
> is some before and after code of what I am contemplating:
>
> *Current:*
> var str:String?
> str = "Hello"
>
> if let str = str {
>     print(str)
> }
>
> *Abbreviated:*
> var str:String?
> str = “Hello”   // Output is Hello, not Optional(“Hello”)
>
> if let str =? {
>     print(str) // Output is also Hello, not Optional(“Hello”)
> }
>
> The value that I perceive of adding this to the language would be to allow
> a short-hand way to unwrap in a “if let" conditional and allowing
> developers to reduce duplication when a unique variable name is not
> needed.  By the way, I’m a big fan of ternary operators which the community
> already sees of value.
>
> Let me know your thoughts.  Is this something that could be put into a
> proposal?
>
> Cheers,
> —Nick—
> .
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160828/70811cd3/attachment.html>


More information about the swift-evolution mailing list