[Issue 5140] Add __FUNCTION__

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 11 01:28:51 PST 2013


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



--- Comment #8 from bearophile_hugs at eml.cc 2013-01-11 01:28:47 PST ---
Sorry Andrej, my last comment wasn't a critique of your good work in pull 1462.

(In reply to comment #7)

> but I don't see __FUNCTION__ as conflicting with __function at all. They do two
> entirely different things. __FUNCTION__ is a string, whereas __function is an
> alias to the function that it's used in.

I agree they are two quite different things, so probably I have to move the
request for __function in another enhancement request.

But they are also related, as you can implement anonymous recursion with
__FUNCTION__:


long fact(long n) {
    if (n <= 1)
        return 1;
    else
        mixin("return n * " ~ __FUNCTION__ ~ "(n - 1);");
}


So I think introducing __FUNCTION__ will make less likely the introduction of
the __function alias. I hope to be wrong.


> Personally, I don't care about anonymous recursion and see no need for them,

Are you using recursion a lot in D? If you never use recursion in D, or you use
it only once in thousand lines of code, then you don't have much need for
language features that help using recursion, like __func (or private default
arguments discussed recently).

If you use recursion often, an alias like __func or __function gets useful,
because it allows you to write more DRY code, you write the function name only
once. So renaming the function or other changes requires you to change only one
name.

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:

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

This D code contains the name Foo only once, this is more DRY, and it's useful.

-- 
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