On Fri, 18 May 2012 16:35:38 -0400, Andrei Alexandrescu <seewebsiteforem...@erdani.org> wrote:

On 5/18/12 1:59 PM, Steven Schveighoffer wrote:
On Fri, 18 May 2012 14:30:46 -0400, Andrei Alexandrescu
<seewebsiteforem...@erdani.org> wrote:

On 5/18/12 1:22 PM, Mehrdad wrote:
My brain just exploded.
Can someone explain what's going on?

class Test
{
public void foo() { }
}

static assert(is(typeof(&Test.foo) == void function()));

Looks like a bug. The assert should pass only if foo were static.

No, this is not a bug.

It is.

Poor design? Yes. Bug? no. It's behavior is very intentional and has been discussed several times over the last several years. It's existed before D2 was even branched, at least since I learned D in 2007.

The purpose is so you can get the function pointer portion of a delegate
without an instance of the object.

Typing is what it is. The following program is unsound without a cast in sight:

class Test
{
     void foo() { writeln("foo"); }
}

static assert(is(typeof(&Test.foo) == void function()));

void fun()
{
     writeln("fun");
}

void main() {
     alias void function() TFun;
     TFun a = &fun;
     a();
     a = &Test.foo;
     a();
}

I agree, it's unsound.  But so is this:

int *blah = void;

*blah = 5;

It doesn't mean that the language should forbid it, or that the compiler isn't implemented as designed.

At the *very least*, the address to member function operation should be illegal in @safe code.

At best things could be arranged that &Test.foo has type void function(Test) or something.

I would suggest that it should be:

function(Test this) with the 'this' being mangled into the name, and affect the calling convention.

Structs would be function(ref Test this).

And const/shared/immutable decorations should apply properly to the 'this' parameter.

I'd wholeheartedly support such an improvement. In fact, I'd be willing to write a DIP on it, if Walter had a chance of approving it. I just don't know if it would happen...

-Steve

Reply via email to