[Issue 9306] New: __function alias
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Jan 12 13:46:16 PST 2013
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 at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at 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: -------
More information about the Digitalmars-d-bugs
mailing list