-
Notifications
You must be signed in to change notification settings - Fork 16
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 a function for selecting a random member of an enum #4
Conversation
Note that if there are no members, it will panic with I have an alternative implementation where it first checks if Returning a slice of either 0 or 1 members would also be a possibility. |
I don't think that this should ever panic, instead there could be an error returned if it's already known what would cause this panic. |
Imagine if the function could return an Optional[M] instead, and Optional was also provided by this or a similar package. Would something along those lines be feasible? |
I think it's a good idea to return a pointer to a member and return a nil pointer for an empty enum. Then it will be consistent with the |
As a side note, I also thought about adding a method like this and a few similar ones but I'm careful not to reinvent genesis. If you want to have some specific operations on the collection of enum members, you can always do something like On the other hand, I don't mind any contributions for which you have a use case. After all, "A little copying is better than a little dependency", I wouldn't want people to bring yet another library to just pick a random enum member or something similar. So, it's a good idea to provide some convenient functions that many different people might need. |
golangci-lint complains about using rand. Feel free to suppress the error, I don't think the function needs to be cryptographically secure or something. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
Add UPD: run |
Perfect! Thank you for the contribution and for sticking with me through all review comments :) |
Thanks for reviewing and merging. Open source software has the potential to benefit us all. 🙂 |
This is potentially useful if there is a large selection of possible settings, as an enum.
Mildly related: https://stackoverflow.com/questions/48490049/how-do-i-choose-a-random-value-from-an-enum