You are browsing as a guest. Sign up (or log in) to start making projects!

3h 58m 59s logged

Operator overloading

Added operator overloading, now you can define overloads for specific operators so that code looks cleaner

Just use the operator keyword with the correct signature and you’re done!

You can overload the operators: +, -, /, *, %, [], []=
For example:

warp operator <T> void T[].set(int index, T item) {        
     self::replace(this, item, index);
}

//use
array[index] = item

//compiler translates to
set(array, index, item)

I also realized that my previous refactor of arrays broke my nullability system:

int[] a = Array(int, 5);
say(a[3]);

This would print null, which my type system missed

I fixed this by moving arrays to a standard library module, the same array import works, but you have to change how you create the array:

//before:
int[] a = Array(int, 5); //doesn't need to be marked as null!!!

int[] a = Array(int, 5);
a[0] = ...;
a[1] = ...;
...

//after
int?[] a = arrayOfNulls(5); //null filled

int[] a = arrayOf(5, (int index) -> {
     return index * 5; //value of the array at that index
});
0
20

Comments 0

No comments yet. Be the first!