Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala : Way to use a function directly into the println(...) using string interpolation

With Scala 2.10+ String Interpolation can be made. So I have this question. How can you do this:

println(f"$foo(100,51)%1.0f" )

considering foo is a function like:

def foo(x1:Int , x2:Int) : Double  = { ... }

From what I understand the way this is evaluated makes foo considered to have no arguments beacause the message I get is :

missing arguments for method foo in object bar;
follow this method with `_' if you want to treat it as a partially applied function

I tried using parenthesis but errors still occur.

like image 315
billpcs Avatar asked Aug 04 '14 21:08

billpcs


1 Answers

Wrap the call to foo in curly brackets.

For instance let

def foo(x1: Int, x2: Int) : Double  = Math.PI * x1 * x2

where for example

foo(100,51)
res: Double = 16022.122533307946

Then

scala> println(f"${foo(100,51)}%1.0f" )
16022
like image 70
elm Avatar answered Sep 22 '22 08:09

elm