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

[lworld] Javac does not allow express casts between value types and their light weight box types

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: repo-valhalla
    • Fix Version/s: repo-valhalla
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      generic
    • OS:
      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).

         Cursor.box boxed = ...
         ((Cursor)boxed).next
      doesn't compile



      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

            • Assignee:
              sadayapalam Srikanth Adayapalam
              Reporter:
              sadayapalam Srikanth Adayapalam
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: