<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Thanks for all the information. This was a cross-post from Stack Overflow:</div><div class=""><br class=""></div><div class=""><a href="http://stackoverflow.com/questions/36061561/generic-factory-method-and-type-inference" class="">http://stackoverflow.com/questions/36061561/generic-factory-method-and-type-inference</a></div><div class=""><br class=""></div><div class="">If you are a Stack Overflow user, you can copy the answer there and I will accept it as correct.</div><div class=""><br class=""></div><div class="">Either way, thanks for the explanation!</div><div class=""><br class=""></div><div class="">R+</div><br class=""><div><blockquote type="cite" class=""><div class="">On 17 Mar 2016, at 16:51, Brent Royal-Gordon <<a href="mailto:brent@architechies.com" class="">brent@architechies.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" class="">final class Something<T> {<br class=""><br class=""> let value: T<br class=""><br class=""> init(initial: T) {<br class=""> value = initial<br class=""> }<br class=""><br class="">}<br class=""><br class="">extension Something {<br class=""><br class=""> class func zip<A, B>(a: A, _ b: B) -> Something<(A, B)> {<br class=""> let initial = (a, b)<br class=""> return Something<(A, B)>(initial: initial)<br class=""> }<br class=""><br class="">}<br class=""><br class="">How come I can’t call zip without explicitly specifying return type?<br class=""><br class="">// ERROR: Cannot invoke `zip` with an argument list of type `(Int, Int)`<br class="">let y = Something.zip(1, 2)<br class=""><br class="">// OK: Works but it’s unacceptable to require this on caller's side<br class="">let x = Something<(Int, Int)>.zip(1, 2)<br class=""></blockquote><br class="">The reason you're seeing this is that there's nothing in this call:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let y = Something.zip(1, 2)<br class=""><br class="">That tells Swift what `T` should be. The return type of the `zip` method is not connected to T; you can actually put any random type in the angle brackets after Something:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let y = Something<UICollectionViewDelegateFlowLayout>.zip(1, 2)<br class=""><br class="">Unfortunately, Swift doesn't currently have the features needed to properly connect `T` to the return type. If the language were more sophisticated, you could say something like this:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>extension<A, B> Something where T == (A, B) {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> class func zip(a: A, _ b: B) -> Something {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> let initial = (a, b)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> return Something(initial: initial)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class="">But for now, you'll have to make do with this horrible hack, which works by meaninglessly reusing the T type parameter:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>extension Something {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> class func zip<B>(a: T, _ b: B) -> Something<(T, B)> {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> let initial = (a, b)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> return Something<(T, B)>(initial: initial)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class="">Hope this helps,<br class="">-- <br class="">Brent Royal-Gordon<br class="">Architechies<br class=""><br class=""></div></div></blockquote></div><br class=""></body></html>