swift - How to call ambiguous method? -
given code
extension array {     func filter(includeelement: (t) -> bool) -> t[] {         var ret = t[]()         e in self {             if includeelement(e) {                 ret += e             }         }         return ret     } }  var = [1,2] var b = a.filter() {i in print(i); return true} it can't compile error message
error: ambiguous use of 'filter' var b = a.filter() {i in print(i); return true}         ^ swift.array<t>:84:8: note: found candidate   func filter(includeelement: (t) -> bool) -> array<t>        ^ <repl>:30:10: note: found candidate     func filter(includeelement: (t) -> bool) -> t[] {          ^ so looks allowed create extension method duplicated method , signature, somehow need special way call
btw, default array.filter broken, calls closure twice each element , crashes repl or give rubbish result in playground if result inconsistent
xiliangchen-imac:~ xiliangchen$ xcrun swift welcome swift!  type :help assistance.   1> let arr = [1,2,3,4,5] arr: int[] = size=5 {   [0] = 1   [1] = 2   [2] = 3   [3] = 4   [4] = 5 }   2> var = 0 i: int = 0   3> let arr2 = arr.filter() {   4.         println($0)   5.         return i++ < 5   6. }    segmentation fault: 11 
there no problem defining ambiguous methods, think. problem arises when import 2 ambiguos methods different modules. unfortunately, there no way how exclude array.filter being imported.
i did tests , appears me behavior ambigious definitions not defined, example:
extension nsstring {     func hasprefix(astring: string!) -> bool {         return false     } }  let string: nsstring = "test"  var hasprefix = string.hasprefix("t") println("has prefix: \(hasprefix)") //prints "true"  var method = string.hasprefix hasprefix = method("t")  println("has prefix: \(hasprefix)") //prints "false" the behavior different obj-c classes...
for functions, appears definition current module preferred:
func nsstringfromcgpoint(point: cgpoint) -> string! {     return "my method" }  var point = cgpoint(x: 10.0, y: 10.0)  println("point: \(nsstringfromcgpoint(point))") //prints "my method" println("point: \(uikit.nsstringfromcgpoint(point))") //prints "{10, 10}" 
Comments
Post a Comment