Scala: a way to use a function directly in println (...) using string interpolation

As of Scala 2.10+ String Interpolation can be done. So I have this question. How can you do it:

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

      

considering foo is a function like:

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

      

From what I understand how this is evaluated, foo is considered to have no arguments since the message I receive 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 parentheses, but errors still happen.

+3


source to share


1 answer


Wrap the call foo

in curly braces.

For example, 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

      

+7


source







All Articles