Referential Datatype || Pointer To An Array Item

36 views
Skip to first unread message

AlienCoder

unread,
Jun 27, 2016, 7:11:24 AM6/27/16
to Haxe

Hello Community,

when I have experimented a little with arrays, I came to the conclusion, that haxe does not treat them as referential datatypes

Example:


var greatArr : Array<String> = new Array<String>();
greatArr[ 0 ] = "Great!";
var referential : String = tollArr[ 0 ];
greatArr[ 0 ] = "Fantastic!";
trace( referential );

For my opinion, the variable   referential   should have the value   "Fantastic", as it should point to the same address like   greatArr[ 0 ]   if it where a referential datatype
 ( or did I get something wrong? )

But actually it is still   "Great!"

So obviously it is not treated as a referential datatype.

Question A: Does this behaviour depend on the target?

I would like to store a nested structures of enum constructors storing arrays of enum constructors storing arrays of...

I also would like to use a variable that stores the present item. This should be just a reference to the array item address (if it would be a referential datatype). Then the present item could be accessed easily via this variable without to surf effortly through the whole nested structure of the array of enums.

Question B: Does anybody have an idea how this could be achieved?

Kind regards
Michael

Jeff Ward

unread,
Jun 27, 2016, 9:48:43 AM6/27/16
to Haxe
Hi Michael,

The array variable (var greatArr)  is referential -- you modify the original array on lines 2 and 4. The String variable (var referential) is not. You made a copy on line 3 (assuming tollArr is just a typo.)

This is the same behavior many languages.

There are some target-specific reference classes in Haxe: search the API for "ref" or "reference", though I've never used them.

Best,
-Jeff

AlienCoder

unread,
Jun 27, 2016, 10:37:27 AM6/27/16
to Haxe

Hello Jeff,

thank you for your answer.

Sorry, I made a mistake. I have translated the strings from german into englisch. And stupidly I also translated the variable   'tollArr'   to   'greatArr'   on one position without translating it at the other.

Actually it is meant to be:


var greatArr : Array<String> = new Array<String>();
greatArr[ 0 ] = "Great!";
var referential : String = greatArr[ 0 ];

greatArr[ 0 ] = "Fantastic!";
trace( referential );

But I have found another error in that thought. I have mixed something up.
What I was looking for is

var greatArr : Array<String> = new Array<String>();
greatArr[ 0 ] = "Great!";

var referential : Array<String> = greatArr;


greatArr[ 0 ] = "Fantastic!";
trace( referential );

So, your answer forced me, to have another look from a different perspective...
;-)

Kind regards
Micheal
Reply all
Reply to author
Forward
0 new messages