Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4151957

Proposal: null-safe field access operator

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.2.0
    • specification
    • sparc
    • solaris_2.6



      Name: bk70084 Date: 06/24/98


      Hi!

      I am proposing a new null-safe operator for field
      access in Java. I'm using "?." for the notation
      in the example below:

        class Base
        {
          Integer member;
        }
          ...

        static Integer getMember( Base b )
        {
          Integer result;

          // Simplest way to code this function will
          // throw a NullPointerException when 'b' is
          // a null object:

          result = b.member;

          // .. or, to avoid, the exception, some code
          // must be added to explicitly check the base
          // object:

          result = b == null ? null : b.member;

          // My suggestion: add operator "?." which
          // returns null if its left-hand-side base
          // object is null and otherwise returns the
          // field referred to by the right-hand-side:

          result = b?.member;

          // E.g. the third assignment works like the
          // second while it looks much like the first
          // assignment.

          // In particular, the third assignment shall
          // never throw a NullPointerException.

          return result;
        }


      Although my proposal is just for convenience and
      has rather obvious workaround, I would be glad if
      you took it into some consideration.

      terv: Risto Lankinen
      (Review ID: 34107)
      ======================================================================

            gbrachasunw Gilad Bracha (Inactive)
            bklocksunw Brian Klock (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: