google / google/protobuf.dart

Consider generating actual `enum`s

Open
#862 0 comments 37 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
572
Forks
196
Avg merge
1h 59m
Merged PRs (30d)
2

Description

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:
```dart
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:
```dart
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()'.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.