Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter() doesn’t preserve type #364

Closed
FGasper opened this issue Jul 10, 2023 · 2 comments
Closed

Filter() doesn’t preserve type #364

FGasper opened this issue Jul 10, 2023 · 2 comments

Comments

@FGasper
Copy link
Contributor

FGasper commented Jul 10, 2023

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 FGasper changed the title Filter() doesn’t preserve type alias Filter() doesn’t preserve type Jul 10, 2023
@jcbritobr
Copy link

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

@FGasper
Copy link
Contributor Author

FGasper commented Jul 15, 2023

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

@samber samber closed this as completed Jun 29, 2024
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

No branches or pull requests

3 participants