-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Split truthy into sub-packages #2
Conversation
Thanks. I have another package with pointer.Deref. I wonder if they should just be combined? Seems related. |
I definitely think that could work! I can mess around with what that could look like later today |
@carlmjohnson just pushed an update! I may have gone overboard but I actually kind of like having everything separated like this - it makes it clearer to me what exactly I'm trying to use |
is/is.go
Outdated
// TruthySlice returns true if the length of the slice is greater than 0. | ||
// Note that it does not distinguish nil slices from empty slices. | ||
func TruthySlice[T any, S ~[]T](v S) bool { | ||
return len(v) > 0 |
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.
is.TruthySlice(s)
is longer than len(s) > 0
. Is the clarity worth it? Maybe if it were a little shorter?
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.
what do you think about e66db9d?
defaults/defaults.go
Outdated
import "github.com/carlmjohnson/truthy/is" | ||
|
||
// GetFirst returns the first value in vs which is non-zero. | ||
func GetFirst[T comparable](vs ...T) (t T) { |
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.
Go 1.22 will have cmp.Or (which I added 😊) that does this same thing, so it's kind of redundant to have it here. OTOH, FirstAny is still useful.
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.
hmm... could it just be removed once it's officially live in Go? that's pretty sweet though
What if the subpackages were bools and pointers? The idea is the standard library has strings, bytes, slices, maps… and we're adding bools and pointers. The bools package would have stuff about truthiness and pointers would be what you have here. |
A bools package could also have bools.Int like this proposal: golang/go#64825. |
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.
Overall, it looks good, but since this is supposed to just be a convenience thing, I think we should push the names to be short as we can without losing clarity. Name suggestions:
- Rename package pointers to ptrs. I think ptr is used as a variable too much to be a package name, but ptrs should be free to use, and it's much shorter.
- bools.Comparable to bools.Value.
- bools.GetFirst and bools.GetFirstAny to bools.Or and bools.AnyOr.
- bools.SetFirst and SetFirstAny to bools.Set and bools.AnySet.
- bools.Ternary to bools.If.
What do you think?
I was excited to find this package! I would find it very useful to have this type of functionality so thought I would add it.