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

[lworld] Javac forbids look ups on the light weight box type of a value type

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • repo-valhalla
    • repo-valhalla
    • tools
    • generic
    • generic

    Description

      Reported by Remi:

      Now, i've tried to play with the .box/.val trying to implement the clojure Cursor protocol,
      which iterate like it's an abstract linked list, so at each step either you return a new Cursor (a value type) or null at the end (which seems to be a good test for a nullable value type).

      The following code compiles !
      I had to had the fix method because the compier refuse to find any methods on Cursor.box


      static value class Tuple {
           private final int index;
           private final int element;
                     private Tuple(int index, int element) {
             this.index = index;
             this.element = element;
           }
         }
            static value class Cursor {
           private final int[] array;
           private final int index;
                private Cursor(int[] array, int index) {
             this.array = array;
             this.index = index;
           }
                Tuple current() {
             return new Tuple(index, array[index]);
           }
                Cursor.box next() {
             if (index + 1 == array.length) {
               return null;
             }
             return new Cursor(array, index + 1);
           }
         }
            private static Cursor.box indexedElements(int[] array) {
           if (array.length == 0) {
             return null;
           }
           return new Cursor(array, 0);
         }
            private static Cursor fix(Cursor.box cursor) {
           return cursor;
         }
            @Benchmark
         public int sum() {
           int sum = 0;
           for(Cursor.box cursor = indexedElements(ARRAY); cursor != null; cursor = fix(cursor).next()) {
             Tuple tuple = fix(cursor).current();
             sum += tuple.index + tuple.element;
           }
           return sum;
         }

      Attachments

        Activity

          People

            sadayapalam Srikanth Adayapalam (Inactive)
            sadayapalam Srikanth Adayapalam (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: