带有隐含的Scala函数式文字 [英] Scala Functional Literals with Implicits
问题描述
我很喜欢用Scala的currying功能来使用implicits。例如,如果我有一个sum函数,并想让第二个参数是一个隐式:
scala> def sum(a:Int)(implicit b:Int)= a + b
sum:(a:Int)(implicit b:Int)Int
有没有办法使用函数值语法来做到这一点?忽略隐含片刻,我通常会编写如下所示的curried函数值:
scala> val sum2 =(a:Int)=> (b:Int)=> a + b
sum:(Int)=> (Int)=> Int =< function1>
然而,第二种方法中的函数签名有很大不同(currying被明确表示)。只需将隐式关键字添加到b没有太大意义,编译器也会抱怨:
scala> val sum2 =(a:Int)=> (implicit b:Int)=> a + b
< console>:1:error:'=>'expected but')'found。
val sum2 =(a:Int)=> (implicit b:Int)=> a + b
^
另外从第一种方法中部分应用总和得到函数值也会导致问题:
scala> val sumFunction = sum _
>
< console>:14:error:找不到参数b的隐式值:int
val sumFunction = sum _
^
这使我相信具有隐含参数的函数必须具有在创建函数值时确定的所述参数,而不是函数 - 值稍后应用。这是真的吗?你可以使用一个带有函数值的隐式参数吗?
感谢您的帮助!
解决方案
scala> val sum2 =(a:Int)=> {implicit b:Int => a + b}
sum2:(Int)=> (Int)=> Int =< function1>
这将使b成为函数体范围的隐式值,因此您可以调用方法这意味着一个隐含的Int。
我不认为你可以有隐式的函数参数,因为它不清楚函数是什么。它是
Int => Int
或()=> Int
?
我发现的最接近的是:
阶> case class Foo(implicit b:Int)extends(Int => Int){def apply(a:Int)= a + b}
defined class Foo
scala>隐式val b = 3
b:Int = 3
scala> Foo()
res22:Foo =< function1>
scala> res22(2)
res23:Int = 5
Forgive me if this has already been asked elsewhere. I have a Scala syntax question involving function-values and implicit parameters.
I'm comfortable using implicits with Scala's currying feature. For instance if I had a sum function and wanted to make the second argument an implicit:
scala> def sum(a: Int)(implicit b: Int) = a + b sum: (a: Int)(implicit b: Int)Int
Is there a way to do this using the function-value syntax? Ignoring the implicit for a moment, I typically write curried function-values like this:
scala> val sum2 = (a: Int) => (b: Int) => a + b sum: (Int) => (Int) => Int = <function1>
However, the function signature in the second approach is much different (the currying is being expressed explicitly). Just adding the implicit keyword to b doesn't make much sense and the compiler complains as well:
scala> val sum2 = (a: Int) => (implicit b: Int) => a + b <console>:1: error: '=>' expected but ')' found. val sum2 = (a: Int) => (implicit b: Int) => a + b ^
Furthermore partially-applying sum from the very first approach to get a function-value causes problems as well:
scala> val sumFunction = sum _ <console>:14: error: could not find implicit value for parameter b: Int val sumFunction = sum _ ^
This leads me to believe that functions that have implicit parameters must have said parameters determined when the function-value is created, not when the function-value is applied later on. Is this really the case? Can you ever use an implicit parameter with a function-value?
Thanks for the help!
解决方案scala> val sum2 = (a: Int) => {implicit b: Int => a + b} sum2: (Int) => (Int) => Int = <function1>
This will just make b an implicit value for the scope of the function body, so you can call methods that expect an implicit Int.
I don't think you can have implicit arguments for functions since then it is unclear what the function is. Is it
Int => Int
or() => Int
?The closest I found is:
scala> case class Foo(implicit b: Int) extends (Int => Int) {def apply(a: Int) = a + b} defined class Foo scala> implicit val b = 3 b: Int = 3 scala> Foo() res22: Foo = <function1> scala> res22(2) res23: Int = 5
这篇关于带有隐含的Scala函数式文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!