www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Does D support tail recursion?

reply Jerry <Jerry_member pathlink.com> writes:
Hi, 
In some ML languages like Ocaml the compiler detects tail-recursions and
automatically changes it to a while loop before assembling into machine code.
Does D have this tail-recursion-optimization too?

-Jerry
Aug 08 2005
next sibling parent Burton Radons <burton-radons smocky.com> writes:
Jerry wrote:
 In some ML languages like Ocaml the compiler detects tail-recursions and
 automatically changes it to a while loop before assembling into machine code.
 Does D have this tail-recursion-optimization too?
No and yes. Tail recursion has two meanings: as a language-mandated optimisation to ensure that a valid tail recursion pattern doesn't result in a stack overflow, and as a simple optimisation pattern that a compiler might recognise. D has the latter meaning: a compiler can apply the optimisation but it's not required to. In a test, DMD does a tail recursion when -O is used, but not without. It didn't do tail recursion if an auto class is used as a local, so it wouldn't do it in the case of any try/finally block either and I don't know what complexity limit there is before it'll abandon the optimisation. It really needs a tail recursion keyword to ensure that if the compiler can't tail-recurse it, then the code is considered erroneous.
Aug 08 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Hello.  Welcome to newsgroups.

Newsreaders allow you to post a single message to two or more newsgroups 
simultaneously.  This is called crossposting, and is the only 
appropriate way of sharing a message across multiple newsgroups.

Please read

http://www.cs.tut.fi/~jkorpela/usenet/xpost.html
http://smjg.port5.com/faqs/usenet/xpost.html

Meanwhile, since you've multiposted this time, please pick one of the 
'groups you've posted to on which you wish the discussion to continue.

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on 
on the 'group where everyone may benefit.
Aug 10 2005