[patterns] on treatment of null

Guy Steele guy.steele at oracle.com
Fri Jul 7 14:13:35 UTC 2017

> On Jul 7, 2017, at 8:56 AM, Maurizio Cimadamore <maurizio.cimadamore at 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.


More information about the amber-spec-experts mailing list