Uploaded image for project: 'Swift'
  1. Swift
  2. SR-985

Nested generic type in function is not allowed

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None

      Description

      This code runs in playground but repl and swiftc reports error:

      type 'Stack' nested in generic function 'foo' is not allowed

      func foo() {
          class Stack<Element> {
              init() {
                  array = []
              }
              init(e: Element) {
                  array = [e]
              }
              init<S: Sequence where S.Iterator.Element == Element>(es: S) {
                  array = [Element](es)
              }
              var array: [Element]
              func push(_ e: Element) {
                  array.append(e)
              }
              func push<S: Sequence where S.Iterator.Element == Element>(_ es: S) {
                  array.append(contentsOf: es)
              }
              func pop() -> Element? {
                  return array.popLast()
              }
              var isEmpty: Bool {
                  return array.isEmpty
              }
          }
          
          func tailRecursive(_ stack: Stack<Int>) {
              guard let i = stack.pop() else { return }
              print(i)
              stack.push(i + 1)
              tailRecursive(stack)
          }
          
          tailRecursive(Stack(e: 0))
      }
      
      foo()
      

        Attachments

          Activity

          Hide
          an0 Ling Wang added a comment -

          Fixed in Swift 3

          Show
          an0 Ling Wang added a comment - Fixed in Swift 3

            People

            • Assignee:
              Unassigned
              Reporter:
              an0 Ling Wang
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: