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

Add KeywordModifierSet helper for conversion to (likely SemIR) enums #4290

Merged
merged 9 commits into from
Sep 10, 2024

Conversation

dwblaikie
Copy link
Contributor

(based on #4272 (comment))

Could haggle over the name "ToEnum" probably avoids the debate over "enumeration" (the type being returned) v "enumerator" (the value being returned)

@@ -195,17 +195,14 @@ static auto BuildFunctionDecl(Context& context,
parent_scope_inst);
bool is_extern = introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extern);
SemIR::FunctionFields::VirtualModifier virtual_modifier =
Copy link
Contributor

Choose a reason for hiding this comment

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

Note, comparing with the above, maybe SemIR::Function instead of SemIR::FunctionFields here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in a58d560

// Return a builder that implicitly converts to the specified enumeration type
// mapping the cases passed to the `Case`s specified. For example:
// ```
// SomeEnum e = set.ToEnumerator(SomeEnum::Default)
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer Enum over Enumerator for brevity, but had you considered an API like StringSwitch:

ToEnum<...>().Case(...).Case(...).Default(...)

This is mainly to make it clearer what the default is. It'd mean making result_ optional and replacing operator T() with auto Default(...) -> T

I think this doesn't even increase verbosity much for call sites because:

SemIR::FunctionFields::VirtualModifier virtual_modifier = modifier_set.ToEnum(...)

becomes:

auto virtual_modifier = modifier_set.ToEnum<SemIR::FunctionFields::VirtualModifier>(...)

(if you're really not a fan of this, you might consider renaming ToEnumerator to ToEnumWithDefault to make it explicit how the argument is used)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to the shorter ToEnum in ae262bd

I liked not having to explicitly specify the type, though, yeah - if it means using auto on the resulting variable the tradeoff is a bit of a wash. And not having to have an implicit conversion is nice - having Default be the terminating operation makes it clearer I think.

Switched to this in 1b6d5cd

@jonmeow
Copy link
Contributor

jonmeow commented Sep 10, 2024

I'm happy to have this, now we can bikeshed over API. 😉

Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

dwblaikie and others added 2 commits September 10, 2024 14:48
@dwblaikie dwblaikie enabled auto-merge September 10, 2024 21:49
@dwblaikie dwblaikie added this pull request to the merge queue Sep 10, 2024
Merged via the queue into carbon-language:trunk with commit b8f61a7 Sep 10, 2024
7 of 8 checks passed
@dwblaikie dwblaikie deleted the modifier_set_to_enumerator branch September 10, 2024 22:20
@jonmeow jonmeow mentioned this pull request Sep 10, 2024
github-merge-queue bot pushed a commit that referenced this pull request Sep 11, 2024
Introduced by #4290

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants