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

feat: Annotate the enum constructor field instead of the class #3

Open
oSumAtrIX opened this issue Nov 10, 2023 · 4 comments
Open

feat: Annotate the enum constructor field instead of the class #3

oSumAtrIX opened this issue Nov 10, 2023 · 4 comments

Comments

@oSumAtrIX
Copy link

oSumAtrIX commented Nov 10, 2023

Currently usage:

image

More intuitive usage:

image

The compiler plugin will have to check if the class that is instantiated with the annotated field is an enum.

@rushiiMachine
Copy link
Member

This doesn't really make sense, also it would introduce global state since I would have to make sure no duplicate FromValues are present on other fields. Not going to be doing this

@oSumAtrIX
Copy link
Author

oSumAtrIX commented Nov 10, 2023

This doesn't really make sense

Please elaborate

also it would introduce global state since I would have to make sure no duplicate FromValues are present on other fields.

This is an implementation difficulty and not an issue with the feature itself.

@rushiiMachine
Copy link
Member

This is for matching and returning enum variants, and annotating a single field doesn't make sense since it's not based on that single field, the generated method is on the entire class

@oSumAtrIX
Copy link
Author

since it's not based on that single field.

The annotation wants you to specify a single field though because it generates the code around that field.

the generated method is on the entire class

Annotating a field does not prevent generating code using the class the field is in.

Annotating the field saves the user from issues like accidentally renaming the field without changing the name in the annotation. Additionally, annotating the field precisely shows that FromValue is generated by comparing the annotated field. Additionally, it simplifies usage because now you do not have to automatically default to the first field, which is an arbitrary decision that you can only guess, nor have to specify the field you want to match to get the correct enum variant as you'd be annotating the field directly.

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

No branches or pull requests

2 participants