feat: add EmptyPathSegment, distinct from PathSegment{} #546
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This might require a little bit more thought than I've put into it so far, but this mainly comes from some code that has
var nextSeg datamodel.PathSegment
, then using that naively when it's not initialised, forString()
in particular. I normally initialise it in a loop using apath.Shift()
operation but withLen()==0
I have the zero value. My assumption was that the zero value would just be empty and I could be fine with that. But even if I did apath.Shift()
on an empty path, I'd get the same thing. Which is, the zero value of aPathSegment
is a0
index. This is probably reasonable in some situations but doing anextSeg.String()
and seeing"0"
was certainly not expected.There is some awkwardness, but it's not new, here's some variations of awkward things you can do, already and with the new thing here:
The two big changes in here are:
EmptyPathSegment
which is""
and distinct from the zero-value ofPathSegment
EmptyPath
as a matching type, which is the same as the zero-value ofPath
EmptyPathSegment
fromPath#Shift()
andPath#Last()
which currently return the zero-value, which is the0
index.This does mean you can't
path.Last() == PathSegment{}
, you'd now have topath.Last() == EmptyPathSegment
. But I think that maybe you should be checking the length first anyway. I've put that in the docs.Which is better?
They both suck, but
Last()
giving you a "zero-index" is a bit more unhelpful than literal""
I think.