[swift-users] Overload Resolution of Binary Operators

Mark Lacey mark.lacey at apple.com
Mon Nov 14 16:45:17 CST 2016


> On Nov 14, 2016, at 2:05 PM, Toni Suter via swift-users <swift-users at swift.org> wrote:
> 
> Hi,
> 
> I would have expected that the following code reports an error, because
> of ambiguous function overloads:
> 
> infix operator ***: MultiplicationPrecedence
> infix operator +++: AdditionPrecedence
> 
> func ***(x: Int, y: Int) -> String {
> 	print("f1")
> 	return ""
> }
> 
> func ***(x: Int, y: Int) -> Int {
> 	print("f2")
> 	return 0
> }
> 
> func +++(x: String, y: Int) -> Int {
> 	print("f3")
> 	return 0
> }
> 
> func +++(x: Int, y: Int) -> Int {
> 	print("f4")
> 	return 0
> }
> 
> let result = 0 *** 4 +++ 0		// prints f2 and f4
> 
> 
> As far as I can tell, there are two possible overload resolutions: f1 + f3 or f2 + f4.
> I thought that these two solutions get an "equivalent score" and therefore there would
> be a compile error. However, that's not the case. Instead, the type checker picks
> f2 and f4.
> 
> So, I guess my question is, whether there is some rule, that prefers
> operators, which have the same argument types and the same return type
> or whether this is simply a bug.

It’s a bug, and one that I’m aware of, but I’m not aware of anything in JIRA for it. Do you mind opening an issue there and assigning it to me?

Mark

> 
> Thanks and best regards,
> Toni
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161114/85b9d980/attachment.html>


More information about the swift-users mailing list