Skip to content

Consider generating actual enums #862

Open
@Levi-Lesches

Description

@Levi-Lesches

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions