chuck icon indicating copy to clipboard operation
chuck copied to clipboard

Static - weird patters

Open celestebetancur opened this issue 1 year ago • 0 comments

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

celestebetancur avatar Mar 09 '23 00:03 celestebetancur