Swift:返回一个In-Out函数

时间:2022-03-18 11:14:39

How do I write a function that returns another function that takes an in-out parameter?

如何编写一个返回另一个带有in-out参数的函数?

I want to write the function makeIncrementor that returns an incrementor function. This incrementor function takes one In-Out parameter and increments it by a certain amount (it doesn't return anything). Here's my code:

我想编写一个函数makeIncrementor,它返回一个incrementor函数。这个incrementment函数接受一个In-Out参数,并以一定数量递增(它不返回任何东西)。这是我的代码:

func makeIncrementor(amount:Int) -> Int->Void {
    func incrementor(inout variable:Int) -> Void {
        variable += amount;
    }
    return incrementor;
}

var x = 1;
var inc = makeIncrementor(2);
inc(&x)
//x should now contain 3

However, Xcode gives the following error:

但是,Xcode给出如下错误:

<REPL>:9:12: error: 'Int' is not a subtype of 'inout Int'
    return incrementor;
           ^

What am I doing wrong?

我做错了什么?

3 个解决方案

#1


1  

The argument list for the function that is being returned should be wrapped in parens, and should include inout before the argument that you want to modify, e.g.

要返回的函数的参数列表应该用括号括起来,并且应该在要修改的参数之前包含inout,例如。

(to see it more clearly, wrap the return value of makeIncrementor in parens as well)

(要看得更清楚,也要将makeIncrementor的返回值用括号括起来)

func makeIncrementor(amount:Int) -> ((inout Int) -> Void) {
    func incrementor(inout variable:Int) -> Void {
        variable += amount;
    }
    return incrementor;
}

#2


1  

You are returning the incrementor function, which has the type (inout Int) -> () while you are declaring your makeIncrementor function returns the type Int -> ()

您正在返回递增函数,它具有类型(inout Int) ->(),而您正在声明makeIncrementor函数返回类型Int -> ()

This mismatch is the reason for your error and changing

这种不匹配是您错误和更改的原因

func makeIncrementor(amount:Int) -> Int-> ()

to

func makeIncrementor(amount : Int) -> (inout Int) -> ()

Is the correct fix. However if you currently try to run that code in a playground it will crash!

是正确的修复。然而,如果您目前试图在一个操场上运行该代码,它将崩溃!

I have successfully run the following code in both OSX and iOS Xcode projects so there are clearly still some stability issues with playgrounds in Xcode.

我已经在OSX和iOS Xcode项目中成功运行了以下代码,因此Xcode中的游乐场显然仍然存在一些稳定性问题。

func makeIncrementor(amount : Int) -> (inout Int) -> () {
    func incrementor(inout variable:Int) {
        variable += amount
    }
    return incrementor
}

var incByTwo = makeIncrementor(2)
var incByThree = makeIncrementor(3)    

var a = 5

println(a) // 5

incByTwo(&a)
println(a) // 7


incByThree(&a)
println(a) // 10 

#3


0  

If you change the return type to:

如果您将返回类型更改为:

func makeIncrementor(amount: Int) -> inout Int -> Void {
    // ...
}

Then the error goes away but my Xcode crashes.

然后错误消失了,但是我的Xcode崩溃了。

#1


1  

The argument list for the function that is being returned should be wrapped in parens, and should include inout before the argument that you want to modify, e.g.

要返回的函数的参数列表应该用括号括起来,并且应该在要修改的参数之前包含inout,例如。

(to see it more clearly, wrap the return value of makeIncrementor in parens as well)

(要看得更清楚,也要将makeIncrementor的返回值用括号括起来)

func makeIncrementor(amount:Int) -> ((inout Int) -> Void) {
    func incrementor(inout variable:Int) -> Void {
        variable += amount;
    }
    return incrementor;
}

#2


1  

You are returning the incrementor function, which has the type (inout Int) -> () while you are declaring your makeIncrementor function returns the type Int -> ()

您正在返回递增函数,它具有类型(inout Int) ->(),而您正在声明makeIncrementor函数返回类型Int -> ()

This mismatch is the reason for your error and changing

这种不匹配是您错误和更改的原因

func makeIncrementor(amount:Int) -> Int-> ()

to

func makeIncrementor(amount : Int) -> (inout Int) -> ()

Is the correct fix. However if you currently try to run that code in a playground it will crash!

是正确的修复。然而,如果您目前试图在一个操场上运行该代码,它将崩溃!

I have successfully run the following code in both OSX and iOS Xcode projects so there are clearly still some stability issues with playgrounds in Xcode.

我已经在OSX和iOS Xcode项目中成功运行了以下代码,因此Xcode中的游乐场显然仍然存在一些稳定性问题。

func makeIncrementor(amount : Int) -> (inout Int) -> () {
    func incrementor(inout variable:Int) {
        variable += amount
    }
    return incrementor
}

var incByTwo = makeIncrementor(2)
var incByThree = makeIncrementor(3)    

var a = 5

println(a) // 5

incByTwo(&a)
println(a) // 7


incByThree(&a)
println(a) // 10 

#3


0  

If you change the return type to:

如果您将返回类型更改为:

func makeIncrementor(amount: Int) -> inout Int -> Void {
    // ...
}

Then the error goes away but my Xcode crashes.

然后错误消失了,但是我的Xcode崩溃了。