--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/91abca90-842b-4c0f-9b71-e382a5e67065%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Aug 5, 2014, at 2:38 PM, Thomas Wetmore <ttwet...@gmail.com> wrote:I've written the following generic protocol for "nodeness" and have a couple concrete classes that implement it, and they work as expected. Now, I would like to write some "generic” routines...
// Perform a depth first recurse through a tree of nodes doing something.
func depthFirst<T: Node> (node: T, visit: (T) -> Int) {
if let children = node.children {
for child in children { depthFirst(child, visit) } // 'T.NodeAlias' is not a subtype of 'T'
^
}
visit(node)
}
So it's back to the drawing board.
Tom
Marco,
I get the same compiler error when building my full executable command line program, when I get rid of the ": Node". In my naive way I chalk it up to "too many things with the same name, e.g., 'count'".
The version as I sent last time is the only one that I have found yet that compiles and runs correctly.
Tom
On Aug 5, 2014, at 9:13 PM, Marco S Hyman wrote:
protocol Node {typealias NodeTypevar parent: NodeType? { get set }}class RealNode: Node {typealias NodeType = RealNodevar parent: RealNode?init(_ p: RealNode? = nil) {parent = p}}
Thomas Wetmore <ttwet...@gmail.com> wrote: You imply that a concrete type that implements the Node protocol must be able to set an instance's parent property to any object that meets the Node protocol. Wouldn't that bring the protocol concept tumbling down?
protocol Node {typealias NodeType: Nodevar children: [NodeType] { get set }
}func depthFirst<T: Node> (node: T, visit: (T) -> Int) {
for child in node.children {depthFirst(child, visit)}visit(node)}
protocol Node {var children: [Self] { get set }}
To meet the Node protocol an implementing type must provide a get/set parent property that must get/set an object meeting the Node protocol. If a RealNode type implements its parent property to get/set a RealNode, that meets the protocol requirement.
protocol Vehicle { … }protocol Passenger {func boardVehicle(v: Vehicle)}class Train : Vehicle { … }class Commuter : Passenger {func boardVehicle(t: Train) {…}}
I don't want to make that restriction [that node trees be homogeneous]. I don't see any reason why the depthFirst algorithm should care one way or the other whether all the nodes it visits in one particular execution be of the same concrete type.
func depthFirst<T: Node>(node: T) {for child in node.children {depthFirst(child as T)}}
protocol Node {var children: [Node] { get set }}func depthFirst(node: Node) {for child in node.children {depthFirst(child)}}class NodeImplementation : Node {var children: [Node] = []}
// Protocol for nodes.
protocol Node {
var tag: String { get set }
var value: String { get set }
var mark: Bool { get set }
var children: [Node] { get set }
var parent: Node? { get set }
}
// Protocol for trees of nodes (actually graphs).
protocol Tree {
var nodes: [Node] { get set }
var root: Node { get set }
}
// Unmark all nodes in a tree/graph.
func unmark (var tree: Tree) {
for node in tree.nodes {
node.mark = false
}
}
// Depth first traverse a tree/graph.
func depthFirst (var tree: Tree, visit: Node -> ()) {
unmark(tree)
depthFirst(tree.root, visit)
}
// Depth first traverse from a node.
func depthFirst (var node: Node, visit: Node -> ()) {
if !node.mark {
for child in children {
depthFirst(child, visit)
}
visit(node)
node.mark = true
}
}