I'm stuck trying to create a linked list of generic nodes using a protocol. I realize there are fifty thousand linked list implementations out there. This is for educational purposes. I've looked over SO for related questions, and read all about protocols and such on swift.org. I've read a few articles about advanced usage on other sites, but I still haven't found something that addresses the problem I'm having.
It seems like a trivial issue; maybe I'm just not asking the right questions? Here's the stripped-down code:
protocol LinkableNode: class {
func getNextNode() -> LinkableNode?
}
class LinkedList<T: LinkableNode> {
var head: T?
func detach() {
head = head!.getNextNode() // The call to getNextNode() is the problem
}
}
I get cannot assign value of type 'LinkableNode?' to type 'T?' on the call to getNextNode(). I want head to be a node too, and I thought T: LinkableNode would do that, but obviously not. I've tried every permutation under the sun based on the few remotely similar examples I've found, but I'm stuck.