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

Proposal: Use generics for underling types #457

Open
ythosa opened this issue May 26, 2024 · 0 comments
Open

Proposal: Use generics for underling types #457

ythosa opened this issue May 26, 2024 · 0 comments

Comments

@ythosa
Copy link

ythosa commented May 26, 2024

As i can see, functions such us IndexOf, Min, Max (all from find.go), Some, Contains (all from intersect.go). Has they have the following semantics:

func X[T someConstraint](slice []T, args ...T) 

I think this is an insufficiently effective use of generics because of we can cover underling type too and users who use the library will not have to cast user types.

For example Contains method will be repesented as

func Contains[S ~[]V, V comparable](slice S, v V) bool {
    for _, e := range slice {
        if e == v {
            return true
        }
    }
    return false
}

instead of

func Contains[V comparable](slice []V, v V) bool {
    for _, e := range slice {
        if e == v {
            return true
        }
    }
    return false
} 

According to my estimates, this is not a breaking change, so we can just improve the lives of library users.

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

1 participant