www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Internally recursion with map

reply Justin Whear <justin economicmodeling.com> writes:
This code fails to compile with a forward reference error:

auto descendantsOf(Node* node)
{
  return join( node.children, join(node.children.map!
(descendantsOf).array) );
}

Obviously, while functions can be internally recursive and call 
themselves, it appears that they can't use themselves as template 
parameters. The Y combinator still blows my mind, so does anyone have any 
ideas about how I could rewrite this without making it super ugly?
Aug 09 2012
parent Justin Whear <justin economicmodeling.com> writes:
On Thu, 09 Aug 2012 22:03:16 +0000, Justin Whear wrote:

 This code fails to compile with a forward reference error:
 
 auto descendantsOf(Node* node)
 {
   return join( node.children, join(node.children.map!
 (descendantsOf).array) );
 }
 
 Obviously, while functions can be internally recursive and call
 themselves, it appears that they can't use themselves as template
 parameters. The Y combinator still blows my mind, so does anyone have
 any ideas about how I could rewrite this without making it super ugly?

Ah, figured it out. If I declare the return type as Node*[] instead of auto and am careful to collapse the ranges to arrays, it does work.
Aug 09 2012