Swift: operator functions don't show normal overloading behavior
| Originator: | garth | ||
| Number: | rdar://23637490 | Date Originated: | 20-Nov-2015 03:10 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 7.1.1 |
| Classification: | Other Bug | Reproducible: | Always |
See: https://forums.developer.apple.com/thread/26621
If this is not a bug, it’s certainly an inconsistent and unexpected behavior. Consider this code:
class A {}
class B: A {}
func +(a: A, b: A) { print("A + A") }
func +(a: A, b: B) { print("A + B") }
func +(a: B, b: A) { print("B + A") }
A() + A() // A + A
A() + B() // A + A
B() + A() // A + A
If the functions were non-operator functions, the calls would be dispatched to the most specific version; there would be one call to each implementation.
What’s especially weird is that defining the B + B version makes everything work as expected, even though the B + B version is never actually called:
class A {}
class B: A {}
func +(a: A, b: A) { print("A + A") }
func +(a: A, b: B) { print("A + B") }
func +(a: B, b: A) { print("B + A") }
func +(a: B, b: B) { print("B + B”) }
A() + A() // A + A
A() + B() // A + B
B() + A() // B + A
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!