On 07/07/17 15:13, Guy Steele wrote:
On Jul 7, 2017, at 8:56 AM, Maurizio Cimadamore
<maurizio.cimadam...@oracle.com> wrote:
Hi,
over the last few weeks we've been exploring the twisted relationship between
patterns and nulls. This document:
http://cr.openjdk.java.net/~mcimadamore/nulls-patterns.html
provides some (hopefully helpful) insights into what the design space looks
like.
tl;dr;
Looks like trying to force the same rule on all patterns, regardless of where
they appear, leads to problems. Distinguishing between toplevel and nested
patterns provides a good basis to handle null in a more predictable/flexible
fashion.
Nice write-up! But there is one other direction that it does not seem to
explore: extending the type system to have explicitly non-null reference types
(an old idea, but perhaps the correct solution for patterns).
String can be String or null
String! cannot be null
if (o matches String! x) { … o cannot be null here … }
if (o matches LinkedList(Object! head, LinkedList tail)) { … head cannot be
null here … }
Not sure it’s where we want to go, but at least it should be explicitly
considered, if only to explicitly reject it.
You are right - there is a connection between the lack of denotability
of non-nullnesss and the failure of some of the options considered in
the document (esp. option 2).
One of the things that leaves a bit of a sour taste is that with '!' in
place, you do have a difference between:
case List l:
case List(...) l:
The former means nullable, the latter means non-nullable (or at least,
should, because you can dereference). So, to be fully consistent, should
it be written as:
case List!(...) l:
?
But generally, yes, this is another piece of the puzzle (and one I
should have called out more explicitly in the doc).
Maurizio
—Guy