Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AST Protocol
protocol AST {
associatedtype NodeType = Self
var children: [NodeType] { get }
}
extension AST where NodeType: AST, NodeType.NodeType == NodeType {
func traverse(check: (Self.NodeType) -> Bool) {
for child in children {
if check(child) {
child.traverse(check: check)
}
}
}
}
class Node: AST {
var children: [Node] = []
}
let n = Node()
n.children = [Node(), Node()]
n.traverse() { node in
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment