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

Consider generating actual enums #862

Open
Levi-Lesches opened this issue Jul 21, 2023 · 0 comments · May be fixed by #931
Open

Consider generating actual enums #862

Levi-Lesches opened this issue Jul 21, 2023 · 0 comments · May be fixed by #931

Comments

@Levi-Lesches
Copy link

Levi-Lesches commented Jul 21, 2023

A Protobuf enum gets generated to a class that extends ProtobufEnum, to ensure they provide name and value. But now with enhanced enums, they can be actual enums and have better switch support, namely "switch expressions", which doesn't allow exhaustiveness with the current implementation.

Currently, the ProtobufEnum class has two final fields that need to be passed to a constructor. Enhanced enums can't extend classes, but they can implement them. So enums can be generated as:

enum MyEnum implements ProtobufEnum {
  a("a", 0), 
  b("b", 1),
  c("c", 2);

  @override final String name;
  @override final int value;
  const MyEnum(this.name, this.value);
}

Which will then enable proper exhaustiveness checking in switch expressions, like the following:

String getName(MyEnum e) => switch (e) {
  MyEnum.a => "a",
  // Error: The type 'MyEnum' is not exhaustively matched by the switch cases since it doesn't match 'MyEnum.b'.
}

Currently, the only warning is just

The type 'MyEnum' is not exhaustively matched by the switch cases since it doesn't match 'MyEnum()'.
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

Successfully merging a pull request may close this issue.

1 participant