Currently Swift provides a static curried function for every instance function
defined. It pretty often allows to avoid closures and be explicit about
functions used. For example:
//flip — flipping an order of arguments for curried functions.
var foo = [[0, 1], [1, 2], [2, 3], [3, 4]].map(flip(Array<Int>.prefix)(1))
//[[0], [1], [2], [3]]
However this behavior is not unified, there is no such behavior available for
variables or constants. For example code like following is impossible as
Array.first is a variable but not a function:
var foo = [[0, 1], [1, 2], [2, 3], [3, 4]].map(Array<Int>.first) //Does not
compile, but should be [0, 1, 2, 3]
I was trying to find what cause to not define static curried functions for
variables and constants and was not able to find a good reason. It is
especially interesting as currently if you define both variable and instance
function with the same name, Swift complains about redeclaration of function,
so it it treat variable already as a function, but does not provide an
alternative way to call it. Moreover following definitions of variable and
static functions a conflicting, as variable definitely has its hidden curried
implementation somewhere but invisible for others:
class Foo {
var bar: Int = 0
static func bar(self: Foo) -> Int {} //Definition conflicts with previous
value
}
One of assumptions why curried functions for variables are hidden — it will
cause ambiguity in code, but I was not able to find examples of ambiguity.
Could someone explain why this behavior is not consistent across functions,
variables and constants? Should we change it to be consistent and make visible
curried static functions for variables and functions? Especially since it is in
a nature of current implementation.
Best,
Nikita Leonov_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution