How Nested Functions Work, part 1

language_fan foo at bar.com.invalid
Wed Sep 2 04:49:24 PDT 2009


Wed, 02 Sep 2009 13:35:05 +0200, Michiel Helvensteijn thusly wrote:

> language_fan wrote:
> 
>>> I agree it would be better to allow forward references for nested
>>> functions, I just wished to point out that the current behavior is not
>>> a bug, it was built that way.
>> 
>> That's great news actually. It might even mean that this might change
>> some day.
> 
> I still find it silly that it was built that way. Seems to me you should
> be able to forward-reference *any* symbol that has a value that can't
> change over its lifetime. Functions, const/immutable vars, typedefs,
> classes, etc.

While the syntax might vary a bit in functional languages, it's a common 
practice for the compiler to solve symbol dependencies inside the 
function scope.

myfun:: Int -> Int -> Int
myfyn a b = result b
   where
   result c = helper3 c + a
   helper3 = helper2 5
   helper2 d c = if c == 1 then d else helper2 d (c - 1)

When I first tried D, I put all my helper functions on top of all 
procedural code inside the function's scope, but soon noticed how badly 
it can fail.



More information about the Digitalmars-d mailing list