2

I have written two helloWorld functions, one with parentheses() and another, without. If I invoke the one with parameters, either specifically with parameters or without, it works fine. The other one defined without parameters cries foul "unit does not take parameters". I am confused.

scala> def hWorld() = println("Hello World")
hWorld: ()Unit

scala> def hWorld = println("Hello World")
hWorld: Unit

scala> hWorld
Hello World

scala> hWorld()
<console>:10: error: Unit does not take parameters
              hWorld()
                    ^
scala> def hWorld2() = println("Hello World")
hWorld2: ()Unit

scala> hWorld2
Hello World

scala> hWorld2()
Hello World

scala> 
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
SriniMurthy
  • 113
  • 2
  • 13

1 Answers1

7

()Unit is a method expecting empty parameter list, Unit is a method expecting no parameter list at all. The first is called nilary, while the second nullary.

Nullary and Nilary Methods

Suma
  • 33,181
  • 16
  • 123
  • 191