digitalmars.D - bug with auto or what?
- Minas Mina (45/45) Aug 13 2012 I'm writing an insert() function for a binary tree in D:
- Chris Cain (9/10) Aug 13 2012 It's not really a bug. You're using insert before its return type
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?
Aug 13 2012
On Monday, 13 August 2012 at 19:09:04 UTC, Minas Mina wrote:Is it a bug with auto or something else?It's not really a bug. You're using insert before its return type has been inferred. Hence, it says "forward reference to inferred return type". However, in this case, it should be possible to infer the return type because they type of root is known. Maybe an enhancement request? In any case, the solution, as you've found, is to explicitly define the return type if you're going to be using it recursively.
Aug 13 2012