I'm writing an insert() function for a binary tree in D:
Note: Node is a value type (struct)
Node!(T)* insert(T) (Node!(T)* root, T val)
{
if( root is null )
{
root = new Node!T();
root.value = val;
root.left = root.right = null;
}
else
{
if( val < root.value )
root.left = insert(root.left, val);
else
root.right = insert(root.right, val);
}
return root;
}
This works (compiles).
auto insert(T) (Node!(T)* root, T val)
{
if( root is null )
{
root = new Node!T();
root.value = val;
root.left = root.right = null;
}
else
{
if( val < root.value )
root.left = insert(root.left, val); // line x
else
root.right = insert(root.right, val); // line y
}
return root;
}
This doesn't compile.
test.d(x): Error: forward reference to inferred return type of
function call insert((*root).left,val)
test.d(y): Error: forward reference to inferred return type of
function call insert((*root).right,val)
Is it a bug with auto or something else?