Quantcast
Channel: Consuming, borrowing and type inference
Viewing all articles
Browse latest Browse all 5

Consuming, borrowing and type inference

$
0
0

The following fails to compile:

struct Ref<T>: ~Copyable {
    var value: T
    init(_ value: T) { self.value = value }
}

func consumingMap<A>(
    _ f: @escaping (inout A) -> Void
) -> (consuming Ref<A>) -> Ref<A> {
    { ref in
        var newRef = ref
        f(&newRef.value)
        return newRef
    }
}

with the error:

'ref' is borrowed and cannot be consumed.  

Shouldn't consuming be inferred here, or am I missing something fundamental? (I feel like I've missed a huge amount of fundamental stuff on move-only types of late).

Read full topic


Viewing all articles
Browse latest Browse all 5

Trending Articles