I'm implementing a simple linked-list stack.
I can get the following Int-only implementaion to work just fine:
class LinkedListIntStack {
class Node {
let item: Int
var next: Node?
init(item: Int) {
self.item = item
}
}
/* ... implementation omitted for brevity ... */
}
Unfortunately, I can't get the same nested construct to work with a generic type:
class LinkedListStack<T> {
class Node<T> {
let item: T
var next: Node?
init(item: T) {
self.item = item
}
}
/* ... omitted for brevity ... */
}
The above fails to compile with the error message:
error: generic type 'Node' nested in type 'LinkedListStack' is not allowed
I can work around this by moving Node<T> outside of LinkedListStack<T>.
However, I find the nested type solution more elegant.
My questions:
- Is it wrong for me to expect this to work? Is there a mistake I'm making and not realizing?
- Is this a design decision or just a current limitation of Swift? If it's the former, what is the motivation behind it?
- Are there alternative solutions you can recommend?
Thanks!