2

I noticed that in the following circumstances, the scala type "Int" can be used, but Any or Nothing, cannot.

user match { case Int => "scala int" case Any => "?" } 

How and why does scala's case class differentiate between these types as being matchable?

1
  • 4
    When you write case Int => you're probably not getting what you expect. It'll only match the Int companion object, which provides e.g. Int.MaxValue, etc. Commented Oct 8, 2014 at 13:52

2 Answers 2

1

Nothing is not really a type at all (there exist no instances of this type - http://www.scala-lang.org/api/2.10.4/index.html#scala.Nothing). To match Any you can just use _

Sign up to request clarification or add additional context in comments.

Comments

1

If you write it correctly:

user match { case _: Int => "scala int" case _: Any => "?" } 

you can use Any (though it isn't useful, as Eugene points out). You can't use Nothing because it could never match.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.