Navigation Menu

Skip to content
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

[SR-8391] Subscript declaration can't use only nested type of generic parameter #50917

Open
natecook1000 opened this issue Jul 27, 2018 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@natecook1000
Copy link
Member

Previous ID SR-8391
Radar None
Original Reporter @natecook1000
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 87f28a6c6c81239df3d0d7c16e641669

Issue Description:

I can define a generic method that only uses nested/associated types of the generic parameter, like the example below. The type C isn't used explicitly in the method signature, but C.Element and C.Index are:

extension Collection {
  func index<C: Collection>(_ i: Index, _ j: C.Index) -> C.Element
    where Element == C
  {
    return self[i][j]
  }
}

That same declaration doesn't work when converted to a subscript, which seems like an unnecessary limitation:

extension Collection {
  subscript<C: Collection>(i: Index, j: C.Index) -> C.Element
    where Element == C
  {
    get {
      return self[i][j]
    }
  }
}
// error: generic parameter 'C' is not used in function signature
//    subscript<C: Collection>(i: Index, j: C.Index) -> C.Element
@belkadan
Copy link
Contributor

I'm not sure you should be allowed to declare the function either. There's no way to call it. cc @DougGregor, @huonw

@belkadan
Copy link
Contributor

Oh, sorry, I misread the declaration. You're adding additional constraints on Self.Element rather than introducing a new type, but we don't have member-level syntax for that.

extension Collection where Element: Collection {
  func index(_ i: Index, _ j: Element.Index) -> Element.Element {
    return self[i][j]
  }
  subscript<Element: Collection>(i: Index, j: Element.Index) -> Element.Element {
    get {
      return self[i][j]
    }
  }
}

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants