What is the difference between Unit and ()Unit

Viewed 94

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> 
1 Answers

()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

Related