Ambiguous method overload with closures in Swift, but only when closure returns a value

后端 未结 1 1111
醉酒成梦
醉酒成梦 2021-01-27 02:29

I found a behaviour in Swift\'s type resolution that I don\'t fully understand. The following example compiles. My understanding is that:

  • the method im
相关标签:
1条回答
  • 2021-01-27 03:22

    You are misunderstanding what's happening. It's not about returning a value. Simplify, for a clearer demonstration of that:

    func call8(_: UInt8) -> String { "8" }
    func call16(_: UInt16) -> String  { "16" }
    
    func ƒ(_ call: (UInt8) -> String) { call(8) }
    func ƒ(_ call: (UInt16) -> String) { call(16) }
    
    ƒ(call8) // "8"
    ƒ(call16) // "16"
    

    Instead, it's all about multiple lines. Read all about it!
    https://forums.swift.org/t/multiline-closure-is-not-inferring-the-return-type/40013

    As for the fix, you've got to explicitly type, for disambiguation.

    0 讨论(0)
提交回复
热议问题