Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:53:06+00:00 2026-06-12T16:53:06+00:00

Scala superimposes a very elegant class hierarchy over Java’s type system, balooning out from

  • 0

Scala superimposes a very elegant class hierarchy over Java’s type system, balooning out from Any at the top, into AnyRef and AnyVal to cover Java’s Objects and primitives respectively, then finally converging, collapsing the reference types onto Null and all types onto Nothing. As I understand it, Nothing is a subtype of everything; Null a subtype of all subtypes of AnyRef/java.lang.Object. [ see http://www.scala-lang.org/node/128 ]

However, there seem to be a few irregularities, a few places where it does not work to simply think of all Scala types as elements of a seamless type hierarchy. I find this irksome, and want to understand the places where I might be surprised.

So far, I know of a few irregularities:

1) Although Null is a subtype of AnyRef, calling null.isInstanceOf[AnyRef] (or other subtypes of AnyRef) returns false. I suspect this was chosen to be consistent with the behavior of Java’s instanceof operator.

2) Everything is covariant to Nothing, regardless of variance annotations. If I have a method that returns a type T that is not marked covariant, I can override that method to return type Nothing. [NOTE: this claim is mistaken, see answers and comments below!]

3) I can’t apply isInstanceOf to the type AnyVal [ See Why can AnyVal not be used in an isInstanceOf check? and How to test a value on being AnyVal? ]

4) It is illegal to ask whether something isInstanceOf[Null], which is a perfectly coherent thing to ask (although not particularly necessary, since “myVar == null” would give the same answer)

Are there other examples of irregularities or special cases in Scala’s type hierarchy? I feel like these are worth learning and understanding to avoid unwelcome surprises.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:53:07+00:00Added an answer on June 12, 2026 at 4:53 pm

    1) "A string is a subtype of AnyRef".isInstanceOf[AnyRef] returns true. This is true for other subtypes of AnyRef as well, except for Null. The only irregularity there is done to be consistent with Java, as you said.

    2) If B is a subtype of A, that is B <: A, then you can always override a method:

    def foo: A = ...
    

    to:

    override def foo: B = ...
    

    This is called refining the return type, and is always allowed. Since Nothing is a subtype of every other type (Nothing <: A for all A), you can always refine your return type to Nothing (e.g. by throwing an exception in the body of the method). This is a pretty regular property. The return type covariance is not directly related to variance annotations on type parameters.

    3) The other questions cover this nicely.

    4) This is because the Null type does not exist in the Java runtime. I guess if you wanted to emulate this, you could create your own instanceOf method – you would first have to check if the argument is null, otherwise, do the normal isInstanceOf check.

    There are other irregularities, yes. See for example: If an Int can't be null, what does null.asInstanceOf[Int] mean?

    Arrays are another example, where you may pay the uniformity of generic arrays with boxing/unboxing or instanceof checks at runtime. The new Array[Any] is translated into an object array – storing an integer into the array will result in boxing it. Whenever you use an Array[T], where T has no upper bound, the array will be patterned matched against the correct runtime array type every time you index an element.

    To better understand how you might be surprised it’s useful to think in terms of how these constructs are translated to the JVM where there is a notion of primitive and reference types, boxing/unboxing and different array classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

scala.math.ScalaNumber is a Java file which looks like this: public abstract class ScalaNumber extends
Scala doesn't have type-safe enum s like Java has. Given a set of related
Scala Where can differences between a class and a type be observed in Scala
In Scala, I am grabbing the type of a given class through its Scala
Scala is very elegant in filtering immutable sequences: var l = List(1,2,3,4,5,6) l =
scala> class A { type T <: String; def f(a: T) = println(foo)} defined
Scala appears to have a ton of features and improvements over Java. I am
Scala (at least on the JVM ) uses type erasure for Java compatibility. This
Scala/Android newbie question. I'm trying to rewrite this simple code from Java to Scala.
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.