www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9306] New: __function alias

http://d.puremagic.com/issues/show_bug.cgi?id=9306

           Summary: __function alias
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-01-12 13:46:15 PST ---
This is a spinoff of Issue 5140 , see there for more info and discussions.

Inside a function __function (or __func) is the alias to the function itself.
It allows code like this, this recursive code will keep working even if "fact"
gets renamed, it's more DRY
(http://en.wikipedia.org/wiki/Don%27t_Repeat_Yourself ):


long fact(long n) {
    if (n <= 1)
        return 1;
    else
        return n * __function(n - 1);
}
void main() {
    assert(fact(20) == 2_432_902_008_176_640_000);
}



See a better explanations and some examples of __function:
http://rosettacode.org/wiki/Anonymous_recursion


It's like the difference between D OOP and Java, this is Java-like code:

class Foo {
    Foo(int x) {...}
    void bar(Foo f) {
        Foo g = new Foo(5);
        ...
    }
}


This is one possible equivalent D code, it contains the name Foo only once:

class Foo {
    this(int x) {...}
    void bar(typeof(this) f) {
        auto g = new typeof(this)(5);
        ...
    }
}


Another use case for __function:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=26404

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 12 2013