加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

斯卡拉 – 关于咖喱功能的问题

发布时间:2020-12-16 18:14:07 所属栏目:安全 来源:网络整理
导读:只是想知道问题w.r.t Currying 如果我们已经定义了curried函数curriedNewSum scala def curriedNewSum(x : Int)(y : Int) = x + ycurriedNewSum: (x: Int)(y: Int)Intscala curriedNewSum(10)(20)res5: Int = 30scala var tenPlus = curriedNewSum(10)_tenPl
只是想知道问题w.r.t Currying

如果我们已经定义了curried函数curriedNewSum

scala> def curriedNewSum(x : Int)(y : Int) = x + y
curriedNewSum: (x: Int)(y: Int)Int

scala> curriedNewSum(10)(20)
res5: Int = 30

scala> var tenPlus = curriedNewSum(10)_
tenPlus: (Int) => Int = <function1>

scala> tenPlus(20)
res6: Int = 30

scala> var plusTen = curriedNewSum(_)(20)
<console>:6: error: missing parameter type for expanded function ((x$1) => curri
edNewSum(x$1)(20))
       var plusTen = curriedNewSum(_)(20)
                                   ^

那么为什么curriedNewSum(10)_工作& curriedNewSum(_)(10)不?

解决方法

我不是100%确定究竟是什么问题,但我强烈怀疑这不是你认为的那样.

比如试试吧

var plusTen = curriedNewSum(_)

你会看到它会返回一个Function1 [Int,Function1 [Int,Int]].现在试试这个:

var plusTen = (curriedNewSum(_))(10)

看到它的工作!嗯,这转化为:

var plusTen = ((x: Int) => curriedNewSum(x))(10)

而另一种方式转化为:

var plusTen = (x) => curriedNewSum(x)(10)

关于函数如何扩展的一些东西搞砸了类型推断.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读