zumzum
zumzum

Reputation: 20228

Swift, Set extension error: Type of expression is ambiguous without a type annotation?

In Swift, I would like to make an extension on Set that takes two arrays, and the function needs to add the elements from one array and remove elements found in another array to the called upon Set.

extension Set {
    
    mutating func update<T>(adding a:[T], removing b:[T]) where T:Hashable {
        var r = union(Set<T>(a)) // ERROR: Type of expression is ambiguous without a type annotation
        r.subtract(Set<T>(b))
        self = r
    }
    
}

I currently get this error: Type of expression is ambiguous without a type annotation.

How can I fix this so that the Set instance I call this on will update accordingly?

Upvotes: 1

Views: 1514

Answers (1)

jnpdx
jnpdx

Reputation: 52555

Instead of defining your own generic T, you should use Element, which is already defined on Set:

extension Set {
    mutating func update(adding a:[Element], removing b:[Element]) {
        var r = union(Set<Element>(a))
        r.subtract(Set<Element>(b))
        self = r
    }
}

Upvotes: 2

Related Questions