fix: relax it.WithoutNth type constraint from comparable to any - #960
Open
deepakganesh78 wants to merge 1 commit into
Open
fix: relax it.WithoutNth type constraint from comparable to any#960deepakganesh78 wants to merge 1 commit into
deepakganesh78 wants to merge 1 commit into
Conversation
it.WithoutNth only operates on indices (nths ...int), never comparing T values, so the comparable constraint is unnecessarily restrictive. This prevents using it with non-comparable types like slices, maps, or funcs. lo.WithoutNth already correctly uses T any. This commit aligns the iterator variant and fixes the doc signatures in both core-withoutnth.md and it-withoutnth.md. Fixes samber#959 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #959
Problem
it.WithoutNthusesT comparabletype constraint, butlo.WithoutNthcorrectly usesT any. SinceWithoutNthonly operates on indices (nths ...int) and never comparesTvalues, thecomparableconstraint is unnecessarily restrictive. This prevents usingit.WithoutNthwith non-comparable types such as slices, maps, or funcs.Reproduction
go seq := slices.Values([][]int{{1, 2}, {3, 4}, {5, 6}}) result := slices.Collect(it.WithoutNth(seq, 1)) // compile error: []int does not satisfy comparableRoot Cause
The
T comparableconstraint onit.WithoutNthwas copied fromit.Without(which genuinely needs it to compare values). ButWithoutNthcallsRejectI(T any) andlo.Keyifyonnths([]int), neither of which requiresTto be comparable.Fix
it.WithoutNthtype parameter fromT comparabletoT anydocs/data/core-withoutnth.md(lo.WithoutNthwas alreadyT anyin code butT comparablein docs)docs/data/it-withoutnth.mdCompatibility
This is a non-breaking change. Loosening a type constraint from
comparabletoanyis backward compatible — all existing code continues to compile, and new code with non-comparable types now also works.Validation
ok github.com/samber/lo 8.391s ok github.com/samber/lo/it 0.536s ok github.com/samber/lo/mutable 0.396s ok github.com/samber/lo/parallel 0.386sgo build ./...,go vet ./..., andgofmtall clean. Added regression testTestWithoutNthNonComparablethat verifies non-comparable types ([][]int) work correctly.