Listing 22.8 map method throw compiler error


Using the code as it is given in the book, the code below throws error: “cannot specialize a non-generic definition”. I am not sure what I am missing out on.

 50> struct Stack<Element> {
 51.     var items = [Element]()
 53.     mutating func push(_ newItem: Element) {
 54.         items.append(newItem)
 55.     }
 57.     mutating func pop() -> Element? {
 58.         guard !items.isEmpty else { return nil }
 59.         return items.removeLast()
 60.     }
 62.     func map<U>(_ f: (Element) -> U) -> Stack<U> {
 63.         var mappedItems = [U]()
 64.         for item in items {
 65.             mappedItems.append(f(item))
 66.         }
 67.         return Stack<U>(items: mappedItems)
 68.     }
 69. }

error: repl.swift:67:16: error: cannot specialize a non-generic definition

Help appreciated. Swift V4, using Swift REPL. Book 2nd ed.


The code you are using there looks great. Can you show us how you are using it after that code in the playground? I copy and pasted your code, then used it, and everything is working well. What version of Xcode are you using?

And here is the pretty much the same in the CLI REPL:


I would have to apologise, since now when I did the whole thing again, it works. Exactly the same code works now. Thanks though for coming back on this.
Since this was actually not a question which should be asked, if you want you can delete the post. Other folks checking out the forum shouldn’t get confused thinking there actually was a compile error here, where there isn’t one.