<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 13, 2016, at 5:37 PM, Hooman Mehr via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 13, 2016, at 3:31 PM, Rick Mann via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Would it make sense to be able to specify priority for a set of overloaded methods to help resolve ambiguity?</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote></div></div></div></blockquote><div><br class=""></div>I don’t think we want to head down that route, partially because using a contextual type as mentioned below removes the ambiguity.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">This might be pretty useful in some situations, but I am not sure if the semantic complexity that it introduces is worth it.</div><div class=""><br class=""></div><div class="">Another example of how this could be useful: </div><div class=""><br class=""></div><div class="">I made a bare-bones <a href="https://gist.github.com/hooman/6e08c48e1e06ee19e06e5b09f664f9be" class="">rational number type</a> for Swift a while ago. I would love to be able to overload “/“ operator to create fractions (rational numbers) instead of dividing two integers. </div><div class=""><br class=""></div><div class="">If I overloaded “/“ to return rational (Int / Int -> Rational), the result type of the operator would become ambiguous for every use of it with integer operands.</div></div></div></blockquote><div><br class=""></div><div>That isn’t the way the type checker works. If you use an explicit type to contextualize the expression, there is no ambiguity. For example this works without any ambiguity.</div><div><br class=""></div><div><div>struct Rational {}</div><div>func / (lhs: Int, rhs: Int) -> Rational { return Rational() }</div><div>func + (lhs: Rational, rhs: Rational) -> Rational { return Rational() }</div><div><br class=""></div><div>func use(r: Rational) {}</div><div><br class=""></div><div>let x: Rational = (1 / 2) + (2 / 3) // Rational result type, no ambiguity</div><div>use(r: (1 / 2) + (2 / 3)) // Rational argument type, no ambiguity</div><div class=""><div class="">let y = (1 / 2) as Rational // Calls func/(Int,Int)->Rational</div></div><div class=""><br class=""></div><div class="">Mark</div></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""> It would be nice if I could prioritize my overload of “/“ over stdlib definition to resolve the ambiguity. </div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>