question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disallow type Null in a type pattern or isInstanceOf test

See original GitHub issue

Should we disallow type Null in a type pattern or isInstanceOf test like scalac does to avoid surprising behaviors?

Dotty:

scala> null.isInstanceOf[Null]
val res0: Boolean = false

Scalac:

scala> null.isInstanceOf[Null]
<console>:12: error: type Null cannot be used in a type pattern or isInstanceOf test
       null.isInstanceOf[Null]
                        ^

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
smartercommented, Feb 15, 2018

I would say yes, same with Singleton.

1reaction
Blaisorbladecommented, Mar 12, 2018

The one with Null might be useful in cases where an isInstanceOf check is inserted by the compiler for some reason (not sure if it ever happens).

So somebody could just forbid isInstanceOf[Null] and see if something breaks. Volunteers? @liufengyun maybe in your PR when you have time?

Pattern matching with singleton types (which is equivalent to isInstanceOf, IIUC) is useful (at least a bit) with GADTs:

def f[T, U](t: Foo[T], u: Foo[U]) =
  t match {
    case _: u.type => // Here T and U are equal
  }

An actual use case is in ECOOP’07 Burak Emir’s paper on case classes, and that code still works today in Scala (though IIRC not yet in Dotty).

Read more comments on GitHub >

github_iconTop Results From Across the Web

scala - why is it not possible to check a type as instance of `Null`
The general contract of x.isInstanceOf[C] is to return true if and only if x is an instance of C . Being "an instance...
Read more >
unchecked pattern binding on type with unsupported ... - GitHub
It seems that adding @unchecked is causing a type test to be generated that wouldn't be otherwise. Below is a diff of -Xprint:typer...
Read more >
Type Pattern Matching with instanceof - nipafx.dev
Type patterns use instanceof and just like that operator, they don't accept null instances. That means you never need to worry whether the ......
Read more >
Documentation - TypeScript 2.0
Null- and undefined-aware types. TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively.
Read more >
Pattern Matching for instanceof - Oracle Help Center
Casting contexts allow the operand of a cast expression (15.16) to be converted to the type explicitly named by the cast operator, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found