Ivan Ivan - 1 month ago 16
Scala Question

One parameter function

If I remove the parentheses from below to read

printFn hc
I get an error. I thought in Scala one parameter functions don't need the parens?

case class HelloClass(arg1:String)


object HelloWorld extends App {
var hc = HelloClass("Hello World")
printFn(hc)

def printFn(a1:HelloClass) : Unit = {
println(a1 arg1) /* or a1.arg1 */
}
}

Answer

I think you mixed it up with the infix method notation. In Scala you can write either obj.method(argument) or obj method argument.

To apply this to your example, you need the "obj" part of the expression, so if you want to avoid parenthesis, you can write

this printFn hc

See also "When to use parenthesis in Scala infix notation".