Does Haxe pass parameters by reference or does it make a copy?
Asked Answered
M

1

6

Take this code:

function createGUIHud():Void
{
    this.screen.gameHud = new NormalGameHud(10, 0, this.screen.getTextureAtlas());
    this.screen.gameHud.x = FlxG.width - (this.screen.gameHud.width + GameSize.getPositionByPlatform(10));
    this.screen.gameHud.y = GameSize.getPositionByPlatform(10);
}

// NormalGameHud.hx

public function new(lives:Int = 10, corn:Int = 0, textureAtlas:SparrowData) 
{
    super(0, 0, 30);
    this.lives = lives;
    this.cornCount = corn;
    this.textureAtlas = textureAtlas;

    this.createScoreboard();
    this.createLivesCount();
    this.createCornCounter();
}

Does textureAtlas get passed by reference or does it get copied?

I know PHP passes objects by reference, and things like Arrays get copied unless stated otherwise (prefixed with &). Does the same apply with Haxe?

Mcdowell answered 30/9, 2015 at 20:25 Comment(0)
H
5

AFAIK, Basic Types (Int, Float, Bool) are passed by value. Everything else is passed by reference.

Hedva answered 30/9, 2015 at 20:44 Comment(2)
"Primitives" can be considered are passed by reference too. It is just they are immutable which means by ref/value does not even matter.Hawsepipe
Actually, I would say that everything in Haxe is passed by value, where basic types (Bool, Int, Float) are copied directly where as objects and other types have their references copied. Bool, Int, Float and String are always immutable and compared by value. Whether or not Strings are copied or have their references copied I think is target specific, but as they are immutable it doesn't really matter.Vesuvianite

© 2022 - 2024 — McMap. All rights reserved.