In D language we can tie function to a template by alias: 
    
    
    struct BinaryTree(T, alias cmp_fun) {
            alias V = T
            alias compare = com_fun;
    }
    
    void main() {
            alias Tree = BinaryTree!(int, (a, b) => a < b);
    }
    
    
    Run

It is no need to store the comparator in the tree object, also it is no need to 
pass the comparison function to other functions. Since it is possible to 
extract the comparison function from the type: 
    
    
    void insert(T)(T tree, T.V value) {
            ...
            if(T.compare(value, node.value)) {...}
    }
    
    
    Run

The same nim code failed to compile: 
    
    
    type
      BinaryTree[V, compare] = object
        value: V
    
    proc insert(tree: BinaryTree, value: BinaryTree.V): bool =
      BinaryTree.compare(value, tree.value) # Error: a type conversion takes 
exactly one argument
    
    var a = BinaryTree[int, proc(a, b: int): bool = a < b](value: 10)
    
    echo insert(a, 20)
    
    
    Run

Reply via email to