-
Notifications
You must be signed in to change notification settings - Fork 27
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
Feature: Check if a given value equals one of the enums #43
Comments
@peter-moran That's pretty easy to write yourself. You can use wise enum to loop over the entries and check, and if you want write a small helper function to encapsulate it. But it's not something I'm likely to merge in. Generally things that are both easy to implement and can easily be implemented "outside" the repo aren't likely candidates; everyone will have their own preferred API/approach for these things and it's pretty low value. The main goal of the repo is to do the "hard" stuff of emulating reflection so users don't have to. |
That is a good point, but that would incur a linear search when an auto generated switch statement would be O(1). Is the string conversion handled as a switch statement or a linear search? |
Enum to string is handled as a switch case. String to enum is handled as linear search. The enum to string switch case is disabled if two enumerators have the same value (because it's ambiguous). An auto generated switch case isn't necessarily going to be more efficient, it really depends. Linear search will be pretty good for small enums, if you want it to be fast the best of course is to ensure contiguous values and just do the two comparisons. Beyond that, if your enum is truly large with values all over the place then a hash table is probably your best bet. This feature could probably be implemented efficiently via my switch case "lift" notion. |
Thanks, I’ll just go with using the linear search then. Interesting idea on the switch case lifts. Thanks for a great library! |
I would like check if a value I have is equivalent to one of my enums.
For example, assume a function
contains
, I would expect it to behave as:The text was updated successfully, but these errors were encountered: