Impossible to call some overloaded functions

I found what might be an issue with the logic of function overloaded when using the some keyword. Given the example code below, I don't think it's possible to call the second makeExample declaration, yet the code compiles fine.

struct Example: Identifiable { let id: Int }

// 1
func makeExample() -> Example {
    Example(id: 0)
}

// 2
func makeExample() -> some Identifiable {
    Example(id: 0)
}

func test() {
    let example: Example = makeExample() // always calls 1
    let example = makeExample() // ambiguous error
}

Doesn't seem to be a big deal, but maybe the compiler should throw an error in the case where you overshadow a function returning an opaque type by overloading it with a concrete type?

A similar problem:

protocol P {}
struct S: P {}
func f<T: P, U: P>(_ v: T) -> U {
    fatalError()
}
func f<T: P, U: P>(_ v: U) -> T {
    fatalError()
}
func test() {
    let x: S = f(S()) // error, Ambiguous use of 'f'
    let y: (S) -> S = f(_:) // error, Ambiguous use of 'f'
}

It seems impossible to call or refer to either version of the overloaded function f.

Terms of Service

Privacy Policy

Cookie Policy