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

Enum Comparison in Setter Does Not Raise Expected Error #11813

Closed
NipponSunrise opened this issue Nov 2, 2024 · 2 comments
Closed

Enum Comparison in Setter Does Not Raise Expected Error #11813

NipponSunrise opened this issue Nov 2, 2024 · 2 comments

Comments

@NipponSunrise
Copy link

NipponSunrise commented Nov 2, 2024

When comparing enums with arguments directly, Haxe raises the expected error:

You cannot directly compare enums with arguments. Use either `switch`, `match` or `Type.enumEq`

However, when this comparison is performed inside a setter, the error is not triggered, and the setter logic does not function as intended. Specifically, the state is updated every time, even if the new value is identical to the current value.

class Test {
	private static var state(default, set):State = Collapsed(true);

	static function main() {
		state = Collapsed(true);
		state = Collapsed(false);
		state = Collapsed(false);
		state = Collapsed(false);
	}

	private static function set_state(v:State):State {
		if (state != v) {
			state = v;
			trace("new value. updated");
		} else {
			trace("old value. not updated");
		}
		return state;
	}
}

enum State {
	Expanded(locked:Bool);
	Collapsed(locked:Bool);
}

Produces the following output:

12:20:05:482   Test.hx:14:,new value. updated
12:20:05:483   Test.hx:14:,new value. updated
12:20:05:483   Test.hx:14:,new value. updated
12:20:05:483   Test.hx:14:,new value. updated

Expected Behavior:

In the setter, the comparison should trigger the same error as in other contexts, enforcing the use of switch, match, or Type.enumEq for enums with arguments.

Actual Behavior:

No error is raised, and the state updates even when the new value matches the current state, resulting in unexpected behavior.

Environment

Haxe 4.3.6 (public playground)

@Simn
Copy link
Member

Simn commented Nov 18, 2024

This has nothing to do with setters, the problem is that this error only triggers when you explicitly compare against Collapsed(true). This is the only case in which the compiler can be sure that you're wrong.

Something we could consider is a flag that detects direct comparisons and complains about it for any enum that has a constructor with arguments. I suppose this could be an optional warning.

@Simn
Copy link
Member

Simn commented Nov 19, 2024

Closed by #11826

@Simn Simn closed this as completed Nov 19, 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

2 participants