Skip to content

fix: relax it.WithoutNth type constraint from comparable to any - #960

Open
deepakganesh78 wants to merge 1 commit into
samber:masterfrom
deepakganesh78:fix/issue959-withoutnth-constraint
Open

fix: relax it.WithoutNth type constraint from comparable to any#960
deepakganesh78 wants to merge 1 commit into
samber:masterfrom
deepakganesh78:fix/issue959-withoutnth-constraint

Conversation

@deepakganesh78

Copy link
Copy Markdown

Fixes #959

Problem

it.WithoutNth uses T comparable type constraint, but lo.WithoutNth correctly uses T any. Since WithoutNth only operates on indices (nths ...int) and never compares T values, the comparable constraint is unnecessarily restrictive. This prevents using it.WithoutNth with 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 comparable

Root Cause

The T comparable constraint on it.WithoutNth was copied from it.Without (which genuinely needs it to compare values). But WithoutNth calls RejectI (T any) and lo.Keyify on nths ([]int), neither of which requires T to be comparable.

Fix

  • Changed it.WithoutNth type parameter from T comparable to T any
  • Fixed doc signatures in docs/data/core-withoutnth.md (lo.WithoutNth was already T any in code but T comparable in docs)
  • Fixed doc signature in docs/data/it-withoutnth.md

Compatibility

This is a non-breaking change. Loosening a type constraint from comparable to any is 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.386s

go build ./..., go vet ./..., and gofmt all clean. Added regression test TestWithoutNthNonComparable that verifies non-comparable types ([][]int) work correctly.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

it.WithoutNth unnecessarily restricts T to comparable (inconsistent with lo.WithoutNth)

1 participant