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

Identity loss when serializing enums #11865

Closed
Simn opened this issue Dec 10, 2024 · 1 comment
Closed

Identity loss when serializing enums #11865

Simn opened this issue Dec 10, 2024 · 1 comment

Comments

@Simn
Copy link
Member

Simn commented Dec 10, 2024

#11864 made the think about how our enum serialization works and I think the way this is handled isn't ideal. Here's some code demonstrating what I mean:

import haxe.Unserializer;
import haxe.Serializer;

enum E {
	C(r:R);
}

typedef R = {
	f:Null<E>
}

function checkIdentity(e:E) {
	switch (e) {
		case C(r1):
			trace(e == r1.f);
	}
}

function main() {
	Serializer.USE_CACHE = true;
	var r = {
		f: null
	};
	var e = C(r);
	r.f = e;
	checkIdentity(e); // traces true
	var s = Serializer.run(e);
	trace(s); // wy1:Ey1:C:1oy1:fwR0R1:1r0g
	var e2:E = Unserializer.run(s);
	checkIdentity(e2); // traces false
}

We can already tell from the serialization string that there are two w entries, even though there's only one enum instance involved. The checkIdentity function confirms the loss of identity.

The existence of a cache mechanism suggests to me that the identity is supposed to be preserved because otherwise we might as well remove it for enums entirely. They can't be recursive by themselves because they are immutable, which is why I'm establishing the recursion through an object.

I would prefer to fix the referencing, but that seems difficult because we don't have the enum instance before we read its arguments.

@Simn
Copy link
Member Author

Simn commented Dec 10, 2024

After looking at this a bit I've concluded that this is not worth looking into. The only way to fix it would be to defer the deserialization of nested structures, so that we can create identities first and then reference them when populating structures. This would be too much of a departure from the current serialization implementation, and perhaps more suited to a custom one.

@Simn Simn closed this as not planned Won't fix, can't repro, duplicate, stale Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant