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
  • 14,658
  • 12
  • 64
  • 124
SriniMurthy
  • 103
  • 1
  • 11

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
  • 31,745
  • 15
  • 120
  • 184