Created
November 17, 2017 16:00
-
-
Save KingOfBrian/c57b4a5b6faa57cf3876ab826c555f68 to your computer and use it in GitHub Desktop.
AST Protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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