Skip to content

spec: adjust scope of type parameters declared by method receivers #52038

Closed

Description

@griesemer

This proposal is about a small correction to the scope of function type parameters.
Per the current spec:

The scope of an identifier denoting a type parameter of a function or declared by a method receiver is the function body and all parameter lists of the function.

The proposal is to change this rule to:

The scope of an identifier denoting a type parameter of a function or declared by a method receiver starts after the function name and ends at the end of the function body.

There is only a difference for methods of generic types: type parameters declared by method receivers won't be visible in the receiver parameter list anymore. For example, currently we get an error for:

type T[T any] struct {}

func (T[T]) m() {} // error: T is not a generic type

because the T inside the [T] is visible inside the receiver parameter list and shadows the outer T used in front of [T].

If this proposal is accepted, it will be possible to write this code without errors.

See #51503.

Activity

  1. added this to the Go1.19 milestone on Mar 30, 2022
  2. changed the title [-]proposal: spec: [/-] [+]proposal: spec: adjust scope of type parameters declared by method receivers[/+] on Mar 30, 2022
  3. rsc commented on Mar 30, 2022

    @rsc
    Contributor

    Given that type T[T any] struct {} works today, it does seem reasonable to try to make func (T[T]) m() {} work too.

  4. rsc commented on Mar 30, 2022

    @rsc
    Contributor

    This proposal has been added to the active column of the proposals project
    and will now be reviewed at the weekly proposal review meetings.
    — rsc for the proposal review group

  5. self-assigned this
    on Mar 30, 2022
  6. rsc commented on Apr 13, 2022

    @rsc
    Contributor

    Based on the discussion above, this proposal seems like a likely accept.
    — rsc for the proposal review group

  7. rsc commented on May 4, 2022

    @rsc
    Contributor

    No change in consensus, so accepted. 🎉
    This issue now tracks the work of implementing the proposal.
    — rsc for the proposal review group

  8. changed the title [-]proposal: spec: adjust scope of type parameters declared by method receivers[/-] [+]spec: adjust scope of type parameters declared by method receivers[/+] on May 4, 2022
  9. gopherbot commented on May 11, 2022

    @gopherbot
    Contributor

    Change https://go.dev/cl/405754 mentions this issue: spec: adjust scope of function/method type paramaters

  10. locked and limited conversation to collaborators on Jun 7, 2022
  11. moved this to Accepted in Proposalson Aug 10, 2022
  12. removed this from Proposalson May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions