lo icon indicating copy to clipboard operation
lo copied to clipboard

Filter() doesn’t preserve type

Open FGasper opened this issue 1 year ago • 2 comments

package main

import (
	"fmt"

	"github.com/samber/lo"
)

type intSlice []int

func main() {
	s1 := intSlice{3, 4, 5}

	s2 := lo.Filter(s1, func(_ int, _ int) bool { return true })
	i1 := any(s2)

	s3 := i1.(intSlice)
	fmt.Printf("s3: %#v", s3)
}

^^ The above panics because s2 is of type []int, not intSlice.

Filter() seems like it should return intSlice here instead.

FGasper avatar Jul 10 '23 16:07 FGasper

Thats because intslice is already a slice. You will get a slice of slices.

jcbritobr avatar Jul 15 '23 13:07 jcbritobr

@jcbritobr See the linked PR for a fix with test.

FGasper avatar Jul 15 '23 14:07 FGasper