-
Bug
-
Resolution: Fixed
-
P4
-
repo-valhalla
-
generic
-
generic
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;
}
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;
}