Skip to content

proposal: Add member access to structure generics #73012

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
2 of 4 tasks
Guest-615695028 opened this issue Mar 23, 2025 · 1 comment
Closed
2 of 4 tasks

proposal: Add member access to structure generics #73012

Guest-615695028 opened this issue Mar 23, 2025 · 1 comment
Labels
LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal
Milestone

Comments

@Guest-615695028
Copy link

Go Programming Experience

Experienced

Other Languages Experience

C/C++, JavaScript

Related Idea

  • Has this idea, or one like it, been proposed before?
  • Does this affect error handling?
  • Is this about generics?
  • Is this change backward compatible? Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit

Has this idea, or one like it, been proposed before?

No

Does this affect error handling?

No

Is this about generics?

Yes.

  1. any value of generic number type can be calculated, compared or assigned from literal.
  2. any value of generic slice type (~[]E) can have its member accessed, but not struct type.

Proposal

Allow mamber access for generic struct type values.

Language Spec Changes

This will allow mamber access for generic struct type values.

Informal Change

No response

Is this change backward compatible?

Yes. This is a brand-new concept.

Orthogonality: How does this change interact or overlap with existing features?

No response

Would this change make Go easier or harder to learn, and why?

Easier. The error "r.Pix undefined (type I has no field or method Pix)" will not exist anymore.

// Generic base for many image types, such as CMYK, (N)RGBA(64), Gray(16), Alpha(16)
type BasicImage = struct {
	// Pixel data
	Pix []uint8
	// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
	Stride int
	// Rect is the image's bounds.
	Rect image.Rectangle
}

func Canonize[I BasicImage](im I, n int) *I {
	pix := im.Pix // shallow copy for reading
	rect := im.Rect
	x, y := rect.Dx()*n, rect.Dy()
	r := I{
		Pix:    make([]uint8, 0, x*y),
		Stride: x,
		Rect:   rect.Sub(rect.Min),
	}
	for i := range y {
		copy(r.Pix[i*x:(i+1)*x], pix[i*im.Stride:])
	}
	return &r
}

Cost Description

No response

Changes to Go ToolChain

No response

Performance Costs

No response

Prototype

No response

@Guest-615695028 Guest-615695028 added LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal labels Mar 23, 2025
@gopherbot gopherbot added this to the Proposal milestone Mar 23, 2025
@gabyhelp
Copy link

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal
Projects
None yet
Development

No branches or pull requests

4 participants