Ivan Ivan - 1 year ago 109
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")

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

Answer Source

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".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download