9

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.

billpcs
  • 633
  • 10
  • 17

1 Answers1

16

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
elm
  • 20,117
  • 14
  • 67
  • 113
  • Thank you very much. I just did not want to use a val in my program so that it can be fully Functional. – billpcs Aug 04 '14 at 21:30
  • What about `val` makes it non-functional? – Larsenal Aug 04 '14 at 21:33
  • No use of local variables. – billpcs Aug 04 '14 at 21:35
  • 5
    @DoomProg You're mistaken as to what that means. A `val` is not a variable - it is a value. A variable can change, a value cannot. Functional languages bind local values all the time - look at any ML family program and you're bound to see a lot of `let` clauses to declare immutable local values. You can feel free to use `val` as much as you like in Scala without fear of breaking a functional style. – KChaloux Aug 04 '14 at 21:47
  • I'd also add that I'm often glad I have distinct line numbers when I get a gnarly stack track in my error log. – Larsenal Aug 04 '14 at 22:10
  • 1
    @DoomProg To follow on @KChaloux, the big issue is with mutability, especially when it's observable from outside a function/object. Functional programming emphasizes referential transparency, which is the property of always returning the same result given the same parameters. It is possible to use `var` and still preserve R.T., although I can't think of a time when this would be necessary. I typically use lots of `val`s to break up my code into easily understandable steps. There's no award for most operations per line :) – acjay Aug 04 '14 at 22:16
  • Thank you all for these clarifications! I fully understand your point. – billpcs Aug 05 '14 at 10:28