Skip to content

proposal: Add member access to structure generics #73013

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 · 0 comments
Closed
2 of 4 tasks

proposal: Add member access to structure generics #73013

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

Comments

@Guest-615695028
Copy link

Guest-615695028 commented Mar 23, 2025

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 with, compared to, or assigned from either a number literal or another value of the same type.
  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, and nothing older is affected.

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

Generic struct types.

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

Nearly no cost.

Changes to Go ToolChain

0

Performance Costs

Not measured

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
@seankhliao seankhliao marked this as a duplicate of #73012 Mar 23, 2025
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

3 participants