println(null.asInstanceOf[Int])

It gives 0 instead of a compiling error.

Is this by design?

Why do you expect a compiling error? If you force the compiler with asInstanceOf[Int]) to interpret null as Int, the result 0 seems not so strange to me. What do I miss here?

It is in the spec: Expressions

Key word: “default value”

2 Likes

I was using a Source.future(Future.success(null.asInstanceOf[Int])) and then the spec failed, which supprised me, and then I change the code to `Source.future(Future.success(null.asInstanceOf[String]))`

I think Int is a sub type of the AnyVal and Null is a sub type of AnyRef, so I expect an error.

Ah, i see. Thank you for taking the time to explain.

1 Like