Skip to content

proposal: generics: type parameters structs with fields does not carry fields #71477

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

Closed
MathSO opened this issue Jan 29, 2025 · 2 comments
Closed
Labels
Milestone

Comments

@MathSO
Copy link

MathSO commented Jan 29, 2025

Proposal Details

When using struct with fields as type parameters the Type does not have the fields from the struct

func GetName[Type ~struct{ Name string }](obj Type) string {
	return obj.Name
}

One must cast it to an other struct type in order to get access to that fields

func GetName[Type ~struct{ Name string }](obj Type) string {
	converted := (struct{Name string})(obj)
	return converted.Name
}

Proposal to have fileds direct on type parameters without need for conversion

@gopherbot gopherbot added this to the Proposal milestone Jan 29, 2025
@zigo101
Copy link

zigo101 commented Jan 30, 2025

#48522

@zigo101
Copy link

zigo101 commented Jan 30, 2025

More specific: #51576

This is just one restriction of the current Go generics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants