ayan ahmedov
ayan ahmedov

Reputation: 391

Dummy placeholder

How do I make a return type which is a subtype of all other types. I want the following

def foo(): (String => String) = TODO
def bar(): String = TODO
def baz(): Int = TODO

What type should TODO be?

Upvotes: 0

Views: 155

Answers (2)

serejja
serejja

Reputation: 23851

"A subtype of all other types" in Scala is Nothing. You may do something like this:

def foo(): (String => String) = TODO

def bar(): String = TODO

def baz(): Int = TODO

def TODO: Nothing = throw new UnsupportedOperationException("not implemented")

EDIT:

OK, as OP accepted my answer for a strange reason and people keep upvoting it (for an even more strange reason) I feel responsible to change my answer. All credits go to om-nom-nom:

The thing you are looking for is already implemented in Scala Standard Library and works like this:

def foo(): Int = ???

Upvotes: 5

Dirk
Dirk

Reputation: 31053

Nothing is a subtype of every type, which has no habitants. It's the type of a throw, so simply implement tge method body by throwing

def todo(): Nothing = throw new NotYetException

Upvotes: 0

Related Questions