[swift-dev] [PITCH] Improve Optional Chain Assignment on Tuple Assignment

John McCall rjmccall at apple.com
Sat Dec 16 01:24:46 CST 2017


> On Dec 16, 2017, at 2:21 AM, Cao, Jiannan via swift-dev <swift-dev at swift.org> wrote:
> 
> Hi all,
> 
> I have come up an improvement point about Optional Chain Assignment
> 
> 1. Optional Chain Assignment not working with tuple assignment
> 
> optional chain assignment only work for directly assignment, not working with tuple assignment.
> 
> For example:
> 
> struct ListNode {
>     var next: ListNode?
> }
> 
> var previous: ListNode?
> var current: ListNode? = ListNode()
> 
> //original version
> let temp = current?.next
> current?.next = previous
> previous = current
> current = temp
> 
> // tuple assignment version (currently compiler error)
> (current?.next, previous, current) = (previous, current, current?.next)
> 
> error: cannot assign to immutable expression of type '_?'
> (current?.next, previous, current) = (previous, current, current?.next)
>  ~~~~~~~~~~~~~                     ^
> 
> Should we improve this situation?

Optional chain assignment normally prevents the RHS of the assignment from being evaluated if the chain is aborted.  What do you recommend as the semantics here?  What if the optional element is later in the sequence?

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20171216/503cbc53/attachment.html>


More information about the swift-dev mailing list