What is the difference between Unit and ()?

I thought () was the only instance of Unit in Scala.

When I try to set the function to the anonymous variable func, this works:

def some(a:Unit):Unit = {
    println("Enigma")
}
val func:Unit => Unit = some
func()

But this is not so:

def some():Unit = {
    println("Enigma")
}
val func:Unit => Unit = some
+4
source share
3 answers

An empty parameter list, as in your second example, does not match a unit, which is a value that represents something like zero. () Does not always mean Unit in every context, in particular, the instance in which you are dealing with a list arguments.

+3
source

This is because your second example is a method with no arguments.

Take a look at the types that produce the results:

some: (a: Unit)Unit
some: ()Unit

which will be written in the type assignment as

Unit => Unit
() => Unit

parens Unit, . () Unit . Unit, , ()

+1
def some(): Unit

() Unit, .

()

def some(): Unit = ()

Some() Scala,

scala> Some()
res0: Some[Unit] = Some(())

, , , Scala Some() Some(()). , , .

0

All Articles