Skip to content

Conversation

fawdlstty
Copy link

example fbs file:

enum Foo : ubyte {
  Bar0 = 0,
  Bar1,
  Bar2,
}

before change:

@override
String toString() {
  return 'Foo{value: $value}';
}

after change:

@override
String toString() {
  switch (value) {
    case 0: return "Bar0";
    case 1: return "Bar1";
    case 2: return "Bar2";
    default: "";
  }
}

@github-actions github-actions bot added c++ codegen Involving generating code from schema dart labels Mar 15, 2024
Comment on lines +284 to +292
code += " switch (value) {\n";
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
const auto enum_var = namer_.Variant(ev);
code += " case " + enum_def.ToString(ev) + ": return \"" + enum_var +
"\";\n";
}
code += " default: return \"\";\n";
code += " }\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed format loses the information about the containing enum. What's the reasoning behind that proposal?

Also, if we should change this, i'd suggest aligning with dart Enum toString() instead:

enum Color { red, green, blue }

main() {
  test('test', () {
    expect(Color.red.toString(), 'Color.red');
  });
}

Additionally, that would seem like a good time to switch to actual enums. Do you think we'd be able to do all that's needed to construct in the scope of flatbuffers if we use enhanced enum syntax?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I've looked into enhanced enums and it works fine, even with the default toString. I'll prep a PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vaind It seems that there will also be issues with #5467

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current code already handles that and throws in such a case: throw StateError('Invalid value $value for bit flag enum Abc');. Whether that's the best thing to do is a different topic.

the behavior remains unchanged after the change to enhanced enums (#8313)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See what others think. If the new PR is merged, I will close this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ codegen Involving generating code from schema dart
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants