Sarafri
Sarafri

Reputation: 23

Why "def put(x: Int): Unit" works, but "def put(x: Int): ()" doesn't?

Why does the following work where Unit is the return type

def put(x: Int): Unit

while putting () as the return type

def put(x: Int): ()

gives => expected error. What is the difference between the two signatures?

Upvotes: 1

Views: 172

Answers (1)

Mario Galic
Mario Galic

Reputation: 48410

Note that () is a value of the type Unit, similar to how 7 is a value of the type Int, so writing

def put(x: Int): ()

is a bit like writing

def f(x: Int): 7

where we have erroneously put a value in the position where return type is expected.

Upvotes: 8

Related Questions