[Accepted with modifications] SE-0432: Borrowing and consuming pattern matching for noncopyable types

The review of SE-0432: Borrowing and consuming pattern matching for noncopyable types has concluded and the proposal has been accepted with modifications.

Review feedback was light, but did raise the concern over this proposal elevating the status of stored properties as distinct from computed properties in being borrowable. The language steering group acknowledges that this highlights the gap that is expect to be filled by read coroutines. The addition of borrowing bindings only in switches rather than as a general feature also seemed undesirable.

As such, the proposal authors agreed to subset out the explicit borrowing syntax from the proposal, leaving it as a future direction pending wider support in the language. The proposal revision can be found here.

Thank you for everyone who participated in the pitch and review!

Ben Cohen
Review Manager

9 Likes

case conditions in if, while, for, and guard

Patterns can also appear in if, while, for, and guard forms as part of case conditions, such as if case <pattern> = <subject> { }. These behave just like switches with one case containing the pattern, corresponding to a true condition result with bindings, and a default branch corresponding to a false condition result. Therefore, the ownership behavior of the case condition on the subject follows the behavior of that one pattern.

This sounds to me like I should be able to do the following:

struct Foo: ~Copyable {
    let bar: Int
}

func baz(_ foo: borrowing Foo?) {
    if let foo {
        print("Bar: \(foo.bar)")
    }
}

Or at least this:

func baz(_ foo: borrowing Foo?) {
    if case let foo? = foo {
        print("Bar: \(foo.bar)")
    }
}

But both of these don't work. I'm getting an error like this:

error: 'foo' is borrowed and cannot be consumed

Am I just misunderstanding this paragraph or is that not implemented?

1 Like