how to get rid of "cannot deduce function from argument types" elegantly
Christian Köstlin via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 13 15:30:35 PDT 2016
I made a small (could be reduced further) example that creates and walks
a templated binary tree. The tree also gets a factory function to use
type deduction to conveniently construct a tree. Unfortunately this does
not compile, if I remove the three ugly methods between /* these should
go */ comments.
```
fibertest.d(49): Error: template fibertest.tree cannot deduce function
from argument types !()(int, typeof(null), typeof(null)), candidates are:
fibertest.d(41): fibertest.tree(T)(T node, Tree!T left, Tree!T right)
```
Is there a more elegant way to fix this?
Another fix would be to provide 4 factory functions like this (still not
really nice):
```
tree(Tree!T left, T node, Tree!T right) {...}
tree(T node, Tree!T right) {...}
tree(Tree!T left, T node) {...}
tree(T node) {...}
```
```d
import std.concurrency, std.stdio, std.range;
class Tree(T) {
Tree!T left;
T node;
Tree!T right;
this(Tree!T left, T node, Tree!T right) {
this.left = left;
this.node = node;
this.right = right;
}
auto inorder() {
return new Generator!T(() => yieldAll(this));
}
private void yieldAll(Tree!T t) {
if (t is null) return;
yieldAll(t.left);
yield(t.node);
yieldAll(t.right);
}
}
/* these should go */
Tree!T tree(T)(typeof(null) left, T node, typeof(null) right) {
return new Tree!T(null, node, null);
}
Tree!T tree(T)(Tree!T left, T node, typeof(null) right) {
return new Tree!T(left, node, null);
}
Tree!T tree(T)(typeof(null) left, T node, Tree!T right) {
return new Tree!T(null, node, right);
}
/* these should go */
Tree!T tree(T)(Tree!T left, T node, Tree!T right) {
return new Tree!T(left, node, right);
}
void main(string[] args) {
// /3
// /2
// 1
auto t1 = tree(tree(tree(null, 1, null), 2, null), 3, null);
// 1
// \2
// \3
auto t2 = tree(null, 1, tree(null, 2, tree(null, 3, null)));
writeln(t1.inorder());
writeln(t2.inorder());
writeln(t1.inorder().array == t2.inorder().array);
}
```
More information about the Digitalmars-d-learn
mailing list