Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

currying in Swift, new declaration syntax in future?

Tags:

swift

Just installed Swift today in Linux to check it out.

Trying a small example of currying results in a warning that the syntax for currying will change in the future, however I could not find anything about what this new syntax looks like.

The currying example I tried:

func do_stuff(x: Int) (y: Int) (z: Int) -> Int {
    return (x - y) * z
}
let curry_fun = do_stuff(42)
let x = curry_fun(y: 7)(z: 3)

compiling this example results in following warning:

warning: curried function declaration syntax will be removed in a future version of Swift; use a single parameter list
func do_stuff(x: Int) (y: Int) (z: Int) -> Int {
             ^~~~~~~~~~~~~~~~~~~~~~~~~~
                    ,        ,

So what does currying then look like in future swift?

I did try something like func do_stuff(x: Int, y: Int, z: Int) -> Int... , however I couldn't find a way to do currying with that function..

like image 796
Michelrandahl Avatar asked Nov 29 '22 14:11

Michelrandahl


2 Answers

As far as I understand they plan to remove it completely.

Here is a quote from the swift-evolution github repository:

Curried function declaration syntax func foo(x: Int)(y: Int) is of limited usefulness and creates a lot of language and implementation complexity. We should remove it.

like image 26
0x416e746f6e Avatar answered Jan 06 '23 20:01

0x416e746f6e


only the declaration syntax will be removed e.g. func(a: Int)(b:Int) -> Int

func curry(a: Int)(b: Int) -> Int {
    return a + b
}

is equivalent to:

func newCurry(a: Int) -> (b: Int) -> Int {
    return { b in
        return a + b
    }
}
like image 148
nemesit Avatar answered Jan 06 '23 21:01

nemesit