Swift: overload disambiguation is different for arguments vs. return value

Originator:garth
Number:rdar://21800681 Date Originated:13-Jul-2015 02:18 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 7 beta 3
Classification:Other Bug Reproducible:Always
 
It seems to be legitimate to overload functions based on return type. However, the disambiguation rules seem to be different for return values than for function arguments, which seems peculiar. Shouldn’t it be the same?

Below is a set of cases that show the disparity. The put functions, where the type is an argument, all disambiguate smoothly and, to my eye, correctly. However, two of the get functions declare ambiguous matches.

protocol P { }
struct W: P { }
enum Z: Int { case One, Two, Three }

func getValue <T> () -> T?                { print("Called case #1, any type"); return nil }        // #1
func putValue <T> (x: T?)                 { print("Called case #1, any type") }

func getValue <T: P> () -> [T]?           { print("Called case #2, array of <T: P>"); return nil } // #2
func putValue <T: P> (x: [T]?)            { print("Called case #2, array of <T: P>") }

func getValue <Key, T: P> () -> [Key: T]? { print("Called case #3, [Key, T: P]"); return nil }     // #3
func putValue <Key, T: P> (x: [Key: T]?)  { print("Called case #3, [Key, T: P]") }

func getValue <Key: RawRepresentable, T: P> () -> [Key: T]? { print("Called case #4, [Key: RR, T: P]"); return nil } // #4
func putValue <Key: RawRepresentable, T: P> (x: [Key: T]?)  { print("Called case #4, [Key: RR, T: P]") } // #4

var a: W?
var b: [W]?
var c: [Int: W]?
var d: [Z: W]?

putValue(a)    // Calls #1
putValue(b)    // Calls #2
putValue(c)    // Calls #3
putValue(d)    // Calls #4

a = getValue() // Calls #1
b = getValue() // Calls #2
c = getValue() // Error: "Ambiguous use of 'getValue'
d = getValue() // Error: "Ambiguous use of 'getValue'

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!