Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述有没有办法声明一个函数,使我不关心返回值?例如,我有这个数组扩展: extension Array { func forEach(function: (element: T) -> ()) { for e in self { function(element: e) } }} 现在我想做: textFields.forEac 有没有办法声明一个函数,使我不关心返回值?例如,我有这个数组扩展:

extension Array {    func forEach(function: (element: T) -> ()) {        for e in self {            function(element: e)        }    }}

现在我想做:

textFIElds.forEach{
textFIElds.forEach{
extension Array {    func forEach<U>(function: (Element) -> U) {        for e in self {            function(e)        }    }}func f(i: Int)->Int {    return i * 2}func g(i: Int) -> Double {    return Double(0.0)}func h(i: Int) {    println("\(i)")}let a = [1,2,3]a.forEach(g)  // U will be an Inta.forEach(f)  // U will be a Doublea.forEach(h)  // U will be a ()
.resignFirstResponder();return}
.resignFirstResponder()}

我不能因为声明函数返回VoID.

这会解决它:

// function that does something sIDe-effecty,but// if it achIEves some goal,exist earlyfunc someFunc(a: [Int]) {    a.forEach { i -> () in        // goal achIEved early,// return from func        return    }    assert(false)}

但有没有一种通用的方式来声明函数,以便我可以返回任何值或VoID?

谢谢!

解决方法 如果添加第二个没有约束的泛型参数,并键入要返回的函数,则接受任何返回值:

[+++]

但是,我强烈建议你不要这样做,而是用……代替.

在数组上运行函数但不使用返回值的唯一目的是为了副作用.当以更强制性的风格书写并使用副作用和外部状态(而不是更具声明性的功能样式)时,最好使用…作为信号,这就是你正在做的事情.

构建自己的伪控制结构也涉及到令人不快的陷阱.例如,想一想:

[+++]

这是一种常见做法 – 如果实现某些目标(或失败),则尽早切断功能.但是对于forEach结构,这有点误导.它看起来像一个常规循环,但它实际上是一个闭包,并且返回从该闭包返回,准备forEach调用下一个(实质上,它在常规循环中表现得像一个继续).

总结

以上是内存溢出为你收集整理的函数声明返回任何内容,也无效?全部内容,希望文章能够帮你解决函数声明返回任何内容,也无效?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述有没有办法声明一个函数,使我不关心返回值?例如,我有这个数组扩展: extension Array { func forEach(function: (element: T) -> ()) { for e in self { function(element: e) } }} 现在我想做: textFields.forEac 有没有办法声明一个函数,使我不关心返回值?例如,我有这个数组扩展:

extension Array {    func forEach(function: (element: T) -> ()) {        for e in self {            function(element: e)        }    }}

现在我想做:

textFIElds.forEach{
textFIElds.forEach{
extension Array {    func forEach<U>(function: (Element) -> U) {        for e in self {            function(e)        }    }}func f(i: Int)->Int {    return i * 2}func g(i: Int) -> Double {    return Double(0.0)}func h(i: Int) {    println("\(i)")}let a = [1,2,3]a.forEach(g)  // U will be an Inta.forEach(f)  // U will be a Doublea.forEach(h)  // U will be a ()
.resignFirstResponder();return}
.resignFirstResponder()}

我不能因为声明函数返回VoID.

这会解决它:

// function that does something sIDe-effecty,but// if it achIEves some goal,exist earlyfunc someFunc(a: [Int]) {    a.forEach { i -> () in        // goal achIEved early,// return from func        return    }    assert(false)}

但有没有一种通用的方式来声明函数,以便我可以返回任何值或VoID?

谢谢!

解决方法 如果添加第二个没有约束的泛型参数,并键入要返回的函数,则接受任何返回值:

但是,我强烈建议你不要这样做,而是用……代替.

在数组上运行函数但不使用返回值的唯一目的是为了副作用.当以更强制性的风格书写并使用副作用和外部状态(而不是更具声明性的功能样式)时,最好使用…作为信号,这就是你正在做的事情.

构建自己的伪控制结构也涉及到令人不快的陷阱.例如,想一想:

[+++]

这是一种常见做法 – 如果实现某些目标(或失败),则尽早切断功能.但是对于forEach结构,这有点误导.它看起来像一个常规循环,但它实际上是一个闭包,并且返回从该闭包返回,准备forEach调用下一个(实质上,它在常规循环中表现得像一个继续).

总结

以上是内存溢出为你收集整理的函数声明返回任何内容,也无效?全部内容,希望文章能够帮你解决函数声明返回任何内容,也无效?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
函数声明返回任何内容,也无效?_app_内存溢出

函数声明返回任何内容,也无效?

函数声明返回任何内容,也无效?,第1张

概述有没有办法声明一个函数,使我不关心返回值?例如,我有这个数组扩展: extension Array { func forEach(function: (element: T) -> ()) { for e in self { function(element: e) } }} 现在我想做: textFields.forEac 有没有办法声明一个函数,使我不关心返回值?例如,我有这个数组扩展:

extension Array {    func forEach(function: (element: T) -> ()) {        for e in self {            function(element: e)        }    }}

现在我想做:

textFIElds.forEach{
textFIElds.forEach{
extension Array {    func forEach<U>(function: (Element) -> U) {        for e in self {            function(e)        }    }}func f(i: Int)->Int {    return i * 2}func g(i: Int) -> Double {    return Double(0.0)}func h(i: Int) {    println("\(i)")}let a = [1,2,3]a.forEach(g)  // U will be an Inta.forEach(f)  // U will be a Doublea.forEach(h)  // U will be a ()
.resignFirstResponder();return}
.resignFirstResponder()}

我不能因为声明函数返回VoID.

这会解决它:

// function that does something sIDe-effecty,but// if it achIEves some goal,exist earlyfunc someFunc(a: [Int]) {    a.forEach { i -> () in        // goal achIEved early,// return from func        return    }    assert(false)}

但有没有一种通用的方式来声明函数,以便我可以返回任何值或VoID?

谢谢!

解决方法 如果添加第二个没有约束的泛型参数,并键入要返回的函数,则接受任何返回值:

但是,我强烈建议你不要这样做,而是用……代替.

在数组上运行函数但不使用返回值的唯一目的是为了副作用.当以更强制性的风格书写并使用副作用和外部状态(而不是更具声明性的功能样式)时,最好使用…作为信号,这就是你正在做的事情.

构建自己的伪控制结构也涉及到令人不快的陷阱.例如,想一想:

这是一种常见做法 – 如果实现某些目标(或失败),则尽早切断功能.但是对于forEach结构,这有点误导.它看起来像一个常规循环,但它实际上是一个闭包,并且返回从该闭包返回,准备forEach调用下一个(实质上,它在常规循环中表现得像一个继续).

总结

以上是内存溢出为你收集整理的函数声明返回任何内容,也无效?全部内容,希望文章能够帮你解决函数声明返回任何内容,也无效?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1003831.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存