Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with compound assignment operator += in class fields #70

Open
GuilHartt opened this issue Nov 1, 2024 · 4 comments
Open

Problem with compound assignment operator += in class fields #70

GuilHartt opened this issue Nov 1, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@GuilHartt
Copy link

GuilHartt commented Nov 1, 2024

The += operator does not update the value when used in class fields. However, direct assignment such as this.pos.x = this.pos.x + 1 works correctly.

Code exemple:

class Test {
    private var pos:Vector2;

    public function new(x, y:Single) {
        this.pos = new Vector2(x, y);
    }

    public function update():Void {
        this.pos.x += 1; // Doesn't work
        this.pos.x = this.pos.x + 1; // Work
    }
}
@Dimous
Copy link
Contributor

Dimous commented Nov 1, 2024

Hey!
Just an observation.

void Test_obj::update(){
	cpp::Struct<  Vector2 > fh = this->pos; // it's a copy, if only it was a reference -- cpp::Struct<  Vector2 >& fh, it would work
	fh->x = (fh->x + 1);

	this->pos->x = (this->pos->x + 1);
}

All that remains is to find a way to make the transpiler generate it right.

@MAJigsaw77
Copy link
Collaborator

I need to think about a solution hmm

@MAJigsaw77
Copy link
Collaborator

As of now, the reassignment seems to be the only way to mave it working, until there's a proper way to use references

@MAJigsaw77 MAJigsaw77 added the bug Something isn't working label Nov 3, 2024
@Aidan63
Copy link

Aidan63 commented Feb 21, 2025

This is one of those undocumented pitfalls when using cpp.Struct and similar, there's no real way around it. I do have new extern techniques on the way which aims to provide the same functionality and address theses sorts of issues.

HaxeFoundation/haxe#11981

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants