barbara
barbara

Reputation: 3201

Why can't I assign Nothing to a variable of type Unit?

If Nothing is a child class of AnyVal including Unit, then why I cannot assign Nothing to oh?

import scala.Nothing
val oh: Unit = Nothing

Upvotes: 2

Views: 549

Answers (2)

Michael Zajac
Michael Zajac

Reputation: 55569

It's right in the scaladoc.

Nothing is a subtype of every other type (including scala.Null); there exist no instances of this type.

If there are no instances of Nothing, you cannot assign it to anything. It's not a value.

Upvotes: 8

user1804599
user1804599

Reputation:

Nothing is a type, not a value. You can only assign values to variables.

Upvotes: 5

Related Questions