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: generics: allow additional constraints on method. #64846

Open
leaxoy opened this issue Dec 22, 2023 · 6 comments
Open

proposal: generics: allow additional constraints on method. #64846

leaxoy opened this issue Dec 22, 2023 · 6 comments
Labels
generics Issue is related to generics LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@leaxoy
Copy link

leaxoy commented Dec 22, 2023

Proposal Details

Proposal

Generic structures can have type parameters and constraints, and generic structures can also have methods. but there is no way to add more constraints on the methods of generic structures.

Let's look at a classic example, sorting slices:

type Slice[E cmp.Ordered] struct {}

func (s *Slice[E]) Sort() {}

// or 

type Slice[E any] struct {}
func Sort[E cmp.Ordered](s *Slice[E]) {}

// but now, we can't write such code
type Slice[E any] struct {}

func (s *Slice[E cmp.Ordered]) Sort() {}

Some currently feasible methods have some inconveniences

Adding narrower constraints on types also narrows the usefulness of the structure.

Top-level functions will directly affect the user experience, and some chain operations will have to be split into multiple lines.

Therefore I propose that the language should provide the ability to add type constraints on methods of generic structures.


Some possible syntax:

Option 1

using where clause at the end of method signature, but before {

func (s *Slice[E]) Sort() where E: cmp.Ordered {}

This method can only be accessed when E satisfies the constraint cmp.Ordered, otherwise it cannot be accessed.

Other languages, such as c#/rust/swift, provide similar capabilities.

This approach is closer to common practice in other languages, but requires the introduction of a keyword.

Option 2

add constraints near E

func (s *Slice[E cmp.Ordered]) Sort() {}

As far as I know, this approach has not been used in other languages.

This method does not require the introduction of additional keywords, but it looks a bit strange.


This is the complete proposal and some attempts at syntax.

@gopherbot gopherbot added this to the Proposal milestone Dec 22, 2023
@seankhliao
Copy link
Member

Duplicate of #49085

@seankhliao seankhliao marked this as a duplicate of #49085 Dec 22, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 22, 2023
@leaxoy
Copy link
Author

leaxoy commented Dec 22, 2023

These are completely different things. Are you sure you have carefully read the community's proposals? @seankhliao

@leaxoy
Copy link
Author

leaxoy commented Dec 22, 2023

That issue propose add new type parameters, but this is not add new type parameters. It't just narrow the existing type parameter in specific scenarios.

@seankhliao
Copy link
Member

if you consider the fact that the receiver is just another argument to the function, then it's the same: you're introducing new constraints in a method

@ianlancetaylor ianlancetaylor added LanguageChange v2 A language change or incompatible library change generics Issue is related to generics labels Feb 7, 2024
@ianlancetaylor
Copy link
Contributor

This is not a dup. Reopening.

@ianlancetaylor ianlancetaylor reopened this Feb 7, 2024
@ianlancetaylor
Copy link
Contributor

Although this is (now) a dup of #65394, which has more comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generics Issue is related to generics LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

4 participants