Swift 3.0: compiler error when calling global func min<T>(T,T) in Array or Dictionary extension -


after converting swift 2.2 3.0 array extension not compile anymore, because contains call global standard library function min<t>(t,t) , shows compiler error extra argument in call.

here's simple way reproduce error:

extension array {     func smallestint(first: int, second: int) -> int {         return min(first, second) // compiler error: "extra argument in call"     } } 

i same error when adding same function extension of dictionary, while exact same code compiles fine in extension of other types (e.g. string or audiobuffer):

compiler errors in array , dictionary extensions

looking @ documentation of array , dictionary, find there instance methods on sequence named public func min() -> element? , public func min(by areinincreasingorder: (element, element) throws -> bool) rethrows -> element?. while both string , audiobuffer not have kind of min(...) function.

is possible reason why can't call global function? compiler can't distinguish between global func min<t>(t,t) , self.min(...) although have different signatures?

is bug or feature? doing wrong? how can call min(t,t) correctly inside array extension?

i see no reason why compiler shouldn't able resolve function call, therefore consider bug (it has been filed – see sr-2450).

it seems occur whenever attempting call top-level function same name, unambiguously different signature method or property that's accessible same scope in given type (instance or static).

an simpler example be:

func foo(_ a: int) {}  struct foo {      func foo() {} // or static func foo() {}, var foo = 0, static var foo = 0      func bar() {         foo(2) // error: argument passed call takes no arguments     } } 

until fixed, simple solution prefix call name of module in resides in order disambiguate you're referring top-level function, rather instance one. standard library, that's swift:

extension array {     func smallestint(first: int, second: int) -> int {         return swift.min(first, second)     } } 

in swift 4, compiler has better diagnostic error (though fact it's still error bug imo):

extension array {     func smallestint(first: int, second: int) -> int {         // use of 'min' refers instance method 'min(by:)'         // rather global function 'min' in module 'swift'         // - use 'swift.' reference global function in module 'swift'         return min(first, second)     } } 

although what's interesting compiler now warn on attempting call standard library method same name stdlib top-level function:

extension array element : comparable {      func smallest() -> element? {         // use of 'min' treated reference instance method in protocol 'sequence'         // - use 'self.' silence warning         // - use 'swift.' reference global function         return min()     } } 

in case, warning says, can silence using explicit self.:

extension array element : comparable {      func smallest() -> element? {         return self.min()     } } 

although what's curious warning doesn't appear extend non-stdlib defined functions:

func foo(_ a: int) {}  struct foo {      func foo() {}      func bar() {         foo() // no warning...     } } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -