Is there a way to do comparison using enums in Sway?

If you want to compare an enum, one option would be to use a match expression: https://fuellabs.github.io/sway/v0.31.1/basics/control_flow.html#match-expressions

You could also implement the Eq trait for your enum like the example below:

impl core::ops::Eq for MyEnum {
	fn eq(self, other: Self) -> bool {
		match (self, other) {
	       (MyEnum::First, MyEnum::First) => true,
	       (MyEnum::Second, MyEnum::Second) => true,
	       (MyEnum::Third, MyEnum::Third) => true,
	        _ => false,
	    }
    }
}
3 Likes