Disallow type Null in a type pattern or isInstanceOf test
See original GitHub issueShould 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:
- Created 6 years ago
- Comments:9 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I would say yes, same with Singleton.
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: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).