Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Functional Literals with Implicits

Tags:

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!

like image 360
shj Avatar asked Jun 13 '11 02:06

shj


People also ask

Is there a compiler for implicit values in Scala?

In this regard, version 2.8 of Scala introduced a new function in the Predef package, which is always available since the compiler imports it by default: Basically, implicitly works as a “compiler for implicits”. We can verify if there is an implicit value of type T.

What are function literals in Scala?

Scala Function Literals Let's start with the meaning of the term literalin computer science. A literal is a notation for representing a fixed value in source code. We have been using literals for a long time to assign fixed values to different data types. The code below shows some examples.

What's new in Scala 2?

In this regard, version 2.8 of Scala introduced a new function in the Predef package, which is always available since the compiler imports it by default: Basically, implicitly works as a “compiler for implicits”.

What are the advantages and disadvantages of using implicit functions?

The main advantage of using the implicit function is that they remove the boilerplate from the code. that means we can define the name of the implicit function. Then only can use that name without declaring full type. Unnecessary code removal is also an advantage od implicit functions.


1 Answers

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 
like image 87
IttayD Avatar answered Oct 10 '22 04:10

IttayD