Skip to content

proposal: spec: allow unsafe.Alignof and unsafe.Sizeof to take a type literal argument #12994

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
mdempsky opened this issue Oct 19, 2015 · 3 comments

Comments

@mdempsky
Copy link
Contributor

Proposal:

Relax unsafe.Alignof and unsafe.Sizeof to accept either an expression or type literal argument.
If a type literal argument is provided, then they return the alignment or size (respectively) of the specified type.

Arguments for:

  • Simple consistent syntax for finding the alignment or size of a type, regardless of its underlying type.
  • Within the standard library, there are 105 instances of (Align|Size)of\([^)]*(\(0\)|\{\})\).

Arguments against:

  • Just turd polishing; "using package unsafe is going to be ugly anyway".
  • You can already write something like unsafe.Sizeof(*new(T)) if you can't/don't know the underlying type of T.
  • Use cases are much less common outside of the standard library.

Notes:

  • Go's function call syntax already allows the first argument to be a type rather than an expression.
    • Caveat: While the built-in functions make and new take a type instead of an expression, unsafe.Alignof and unsafe.Sizeof would be the first functions to accept either a type or expression.
  • In the expression a.b, a may already be either an expression or a receiver type.
    • Caveat: unsafe.Alignof(x) and unsafe.Sizeof(x) would be the first expressions where x can be either an expression or an arbitrary type literal (i.e., not just T or *T, but also []T, chan T, struct{T}, etc).
  • Because unsafe.Alignof and unsafe.Sizeof are defined as compile-time constant expressions, type checking code already needs to special case them (i.e., as opposed to stubbing them as something like func Sizeof(x interface{}) uintptr).
  • If the untyped composite literal proposal is accepted, parsers/type-checkers/compilers/etc will need to be updated for Go 1.6 anyway.
  • Proof-of-concept CLs:

CC @griesemer

@minux
Copy link
Member

minux commented Oct 19, 2015 via email

@rsc rsc added this to the Proposal milestone Oct 24, 2015
@rsc
Copy link
Contributor

rsc commented Oct 24, 2015

As Minux said, I think this simplifies very few real programs. And I think doing this would only add to the confusion about the semantics, especially given the recent confusion in CL 16004.

I would like to leave things alone.

@rsc rsc changed the title proposal: allow unsafe.Alignof and unsafe.Sizeof to take a type literal argument proposal: spec: allow unsafe.Alignof and unsafe.Sizeof to take a type literal argument Oct 24, 2015
@rsc rsc added the Proposal label Oct 24, 2015
@adg
Copy link
Contributor

adg commented Aug 15, 2016

As this hasn't been responded to since its original proposal, and there is significant objection to the proposal, I am going to close this.

@adg adg closed this as completed Aug 15, 2016
@golang golang locked and limited conversation to collaborators Aug 15, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants