chuck
chuck copied to clipboard
Static - weird patters
This:
// Recommended pattern as hint in the VM
public class Test {
static int StaticArray[];
}
new int[0] @=> Test.StaticArray;
Test.StaticArray << 1;
Test.StaticArray << 2;
<<<"Size: ", Test.StaticArray.cap(), ", Pos 0: " , Test.StaticArray[0], ", Pos 1: ", Test.StaticArray[1]>>>;
This:
//Accessing the value of the static array using the instance
public class Test {
new int[0] @=> static int StaticArray[];
}
Test t;
t.StaticArray << 1;
t.StaticArray << 2;
<<<"Size: ", t.StaticArray.cap(), ", Pos 0: " , t.StaticArray[0], ", Pos 1: ", t.StaticArray[1]>>>;
This:
//This pattern gives the same result but it is weird
public class Test {
new int[0] @=> static int StaticArray[];
}
Test t;
Test.StaticArray << 1; //The array takes values as static
Test.StaticArray << 2;
//values are accessible using the instance
<<<"Size: ", t.StaticArray.cap(), ", Pos 0: " , t.StaticArray[0], ", Pos 1: ", t.StaticArray[1]>>>;
These three examples compile and throw the same result:
Size: 2 , Pos 0: 1 , Pos 1: 2