From a76251bf8aea659f8508db514645c15559a4d78a Mon Sep 17 00:00:00 2001 From: Deepak Ganesh Date: Sat, 1 Aug 2026 22:32:28 +0530 Subject: [PATCH] fix: relax it.WithoutNth type constraint from comparable to any 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 #959 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/data/core-withoutnth.md | 2 +- docs/data/it-withoutnth.md | 2 +- it/intersect.go | 2 +- it/intersect_test.go | 11 +++++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/data/core-withoutnth.md b/docs/data/core-withoutnth.md index e33cb562b..2b6a57225 100644 --- a/docs/data/core-withoutnth.md +++ b/docs/data/core-withoutnth.md @@ -13,7 +13,7 @@ similarHelpers: - core#slice#dropbyindex position: 140 signatures: - - "func WithoutNth[T comparable, Slice ~[]T](collection Slice, nths ...int) Slice" + - "func WithoutNth[T any, Slice ~[]T](collection Slice, nths ...int) Slice" --- Returns a slice excluding the elements at the given indexes. diff --git a/docs/data/it-withoutnth.md b/docs/data/it-withoutnth.md index 912bed811..fc2e439cd 100644 --- a/docs/data/it-withoutnth.md +++ b/docs/data/it-withoutnth.md @@ -5,7 +5,7 @@ sourceRef: it/intersect.go#L253 category: iter subCategory: intersect signatures: - - "func WithoutNth[T comparable, I ~func(func(T) bool)](collection I, nths ...int) I" + - "func WithoutNth[T any, I ~func(func(T) bool)](collection I, nths ...int) I" playUrl: "https://go.dev/play/p/KGE7Lpsk18P" variantHelpers: - iter#intersect#withoutnth diff --git a/it/intersect.go b/it/intersect.go index 603b084c3..80f196972 100644 --- a/it/intersect.go +++ b/it/intersect.go @@ -250,7 +250,7 @@ func WithoutBy[T any, K comparable, I ~func(func(T) bool)](collection I, transfo // WithoutNth returns a sequence excluding the nth value. // Will allocate a map large enough to hold all distinct nths. // Play: https://go.dev/play/p/KGE7Lpsk18P -func WithoutNth[T comparable, I ~func(func(T) bool)](collection I, nths ...int) I { +func WithoutNth[T any, I ~func(func(T) bool)](collection I, nths ...int) I { set := lo.Keyify(nths) return RejectI(collection, func(_ T, index int) bool { return lo.HasKey(set, index) }) } diff --git a/it/intersect_test.go b/it/intersect_test.go index 385982cf9..2d73d8283 100644 --- a/it/intersect_test.go +++ b/it/intersect_test.go @@ -517,6 +517,17 @@ func TestWithoutNth(t *testing.T) { }) } +func TestWithoutNthNonComparable(t *testing.T) { + t.Parallel() + is := assert.New(t) + + // Slices are not comparable; WithoutNth should accept T any (not T comparable) + // since it only operates on indices. + input := slices.Values([][]int{{1, 2}, {3, 4}, {5, 6}}) + result := slices.Collect(WithoutNth(input, 1)) + is.Equal([][]int{{1, 2}, {5, 6}}, result) +} + func TestElementsMatch(t *testing.T) { t.Parallel()