-
Notifications
You must be signed in to change notification settings - Fork 166
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 ulid.Nil
and .IsZero()
method
#112
Conversation
uuid.Nil
and .IsZero()
methodulid.Nil
and .IsZero()
method
What suggests that a ULID of all-zeros is categorically different than any other ULID? Is it valid for code to treat an all-zeroes ULID differently than other ULIDs? related: #109 |
The following reasons suggest that zero-value ULIDs are categorically different:
Given these, in what way are zero value ULIDs not unique? I'm struggling to see any points suggesting that they should be treated the same. To answer the edited question on whether it's valid to check:
Finally, given Go itself treats zero-values as unique, in what way is including a method wrong? It's optional; conforms to the stdlib; conforms to go norms; and, most importantly, allows for zero checks without extra allocations. |
This simplifies comparisons with empty ULIDs. Right now, most people are doing something like the following to check if a ULID is non-zero: ```go id.Compare(ulid.ULID{}) == 0 ``` This requires allocating a new empty ULID each time. Some packages avoid this by creating their own global (or private) nil ULIDs on initialization. This should be built in and simple for better perf & a cleaner API.
acbb2c3
to
0252a33
Compare
I don't really buy any of those points, except for this one:
This is a reasonably compelling argument. A zero-value time is a valid time, and the existence of time.IsZero would support the addition of ulid.IsZero. Thinking... edit:
That says that the zero value of types exists and is a valid value for the type, but it doesn't suggest that it is "categorically different" than any other value of the type, in general. Sometimes it can be, e.g. |
Yep, I agree that ints are different. It's why people shouldn't use 0 in an But when a ULID encodes ms-level time and randomness, an all-zero byte sequence is definitely suspect. |
Let me be clear that I totally understand what you're saying from a pragmatic perspective. I'm trying to weigh that pragmatism against the, I dunno, "correctness" of the package. Correctness only cares about objective stuff like "is this ULID valid per the spec" and can't make any assertions based on subjective stuff like "is this ULID suspect". Hopefully you understand. But I think I'm getting convinced for |
This simplifies comparisons with empty ULIDs. Right now, most people are doing something like the following to check if a ULID is non-zero:
This requires allocating a new empty ULID each time. Some packages avoid this by creating their own global (or private) nil ULIDs on initialization.
This should be built in and simple for better perf & a cleaner API.