Jake
Jake

Reputation: 4660

scala: using function definitions

In the code below, the first function compiles. The second doesn't compile.

type FTDoubleDouble_Double = (Double, Double) => Double
val _dividedBy: FTDoubleDouble_Double =
{
    _ / _.toDouble
}
val _dividedByThenLog: FTDoubleDouble_Double =
{
    val result1 =  _ / _.toDouble
    scala.math.log(result1)
}

The compilation error is

cannot resolve symbol /

I am sure this is basic, but I am a bit confused.

Upvotes: 2

Views: 127

Answers (1)

jwvh
jwvh

Reputation: 51271

In the 1st case the type ascription helps the compiler figure out what the underscores are supposed to represent: 2 Double values. (Which makes the .toDouble cast redundant and pointless.)

The 2nd case doesn't compile because there aren't enough hints to help the compiler.

You can fix that...

val result1 :FTDoubleDouble_Double =  _ / _

...but then you've got another problem.

scala.math.log(result1)  //error

math.log() takes a Double as the passed-in parameter, which you don't have.

Perhaps this is what you want:

val _dividedByThenLog: FTDoubleDouble_Double =
  (d1 :Double, d2 :Double) => scala.math.log(d1 / d2)

Upvotes: 3

Related Questions