[swift-users] Generic factory method and type inference

Rudolf Adamkovič salutis at me.com
Thu Mar 17 08:58:30 CDT 2016


Hello there!

I have the following class with a generic factory method:

final class Something<T> {
    
    let value: T
    
    init(initial: T) {
        value = initial
    }
    
}

extension Something {
    
    class func zip<A, B>(a: A, _ b: B) -> Something<(A, B)> {
        let initial = (a, b)
        return Something<(A, B)>(initial: initial)
    }
    
}

How come I can’t call zip without explicitly specifying return type?

// ERROR: Cannot invoke `zip` with an argument list of type `(Int, Int)`
let y = Something.zip(1, 2)

// OK: Works but it’s unacceptable to require this on caller's side
let x = Something<(Int, Int)>.zip(1, 2)

Thank you for your time!

R+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160317/24d6cda3/attachment.html>


More information about the swift-users mailing list