Bug 8114 – Methods are delegates, not functions.

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-05-18T11:34:00Z
Last change time
2012-10-28T13:47:38Z
Keywords
accepts-invalid, spec
Assigned to
nobody
Creator
wfunction

Comments

Comment #0 by wfunction — 2012-05-18T11:34:43Z
class Test { public void foo() { } } static assert(is(typeof(&Test.foo) == void function()));
Comment #1 by schveiguy — 2012-05-18T12:06:24Z
As horrible as this is, it's intended behavior :) It's the same type you would get if you obtained the funcptr property of a delegate to the same member function. Changing to invalid, if you want to propose a different way to handle it, reopen as an enhancement.
Comment #2 by wfunction — 2012-05-18T13:56:26Z
I understand what you mean, but if that's the case, then the intentional behavior itself is a bug. The *correct* way to achieve what was intended would be to add the 'this' parameter inside the parameter list. Otherwise, it results in buggy code, since the data type of the function doesn't match what it's pointing to.
Comment #3 by bugzilla — 2012-05-18T20:25:43Z
Why is this 'critical'?
Comment #4 by wfunction — 2012-05-18T20:57:40Z
I put it as critical since it seemed looked like a bug in DMD (I'd never seen it before and it didn't make sense), but when people said it was intentional I forgot to change the status after re-opening it. I'll make it normal.
Comment #5 by smjg — 2012-05-19T04:31:01Z
http://dlang.org/type.html#delegates "There are no pointers-to-members in D"
Comment #6 by schveiguy — 2012-05-19T08:28:48Z
(In reply to comment #5) > http://dlang.org/type.html#delegates > "There are no pointers-to-members in D" I think this specifically refers to C++ pointers-to-members which these are not
Comment #7 by smjg — 2012-05-19T08:34:24Z
(In reply to comment #6) > (In reply to comment #5) > > http://dlang.org/type.html#delegates > > "There are no pointers-to-members in D" > > I think this specifically refers to C++ pointers-to-members which these are not What's the difference?
Comment #8 by smjg — 2012-05-19T08:36:13Z
Moreover, I would expect "There are no pointers-to-members in D" to mean "There are no pointers-to-members in D" not "There are pointers-to-members in D, but they aren't of the C++ sort".
Comment #9 by schveiguy — 2012-05-19T09:13:32Z
I'm just saying, that might be what it means, but is just poorly explained. Taking address of member on the class has been around since D1
Comment #10 by smjg — 2012-05-19T09:18:15Z
(In reply to comment #9) > I'm just saying, that might be what it means, but is just poorly explained. > Taking address of member on the class has been around since D1 Maybe. But it can't possibly be intended that type type of &Test.foo is void function(). It would have to be void function(Test). And the spec would have to explicitly allow it.
Comment #11 by wfunction — 2012-05-19T12:20:00Z
(In reply to comment #5) > http://dlang.org/type.html#delegates > "There are no pointers-to-members in D" There aren't though. Pointers to members are data types that relative to a certain class. Functions and delegates aren't relative to any class, they're absolute.
Comment #12 by smjg — 2012-05-19T16:22:55Z
(In reply to comment #11) > (In reply to comment #5) > > http://dlang.org/type.html#delegates > > "There are no pointers-to-members in D" > > There aren't though. Pointers to members are data types that relative to a > certain class. What dictionary are you going by? > Functions and delegates aren't relative to any class, they're absolute. This is only a matter of how C++ happens to notate pointer-to-member types and the dereferencing thereof. (At least a pointer to a member function - a pointer to a member variable is a whole different beast.) Below this level, it's just a function type, the class it's "relative to" just being the type of the implicit this parameter.
Comment #13 by wfunction — 2012-05-19T17:24:47Z
(In reply to comment #12) > What dictionary are you going by? C++'s, since that's the only one I've seen that ever mentions the phrase "pointer to member". :P
Comment #14 by smjg — 2012-05-19T17:49:37Z
(In reply to comment #13) > (In reply to comment #12) > > What dictionary are you going by? > > C++'s, since that's the only one I've seen that ever mentions the phrase > "pointer to member". :P There you go. This explains only what the phrase means in a C++ context. In a more general context, there's nothing to say a "pointer to member" must be a distinct type. Suppose this issue is fixed by implementing the points of comment 10. Then &Test.foo is a pointer to a member of Test. It being just a void function(Test), rather than some other, distinct but semantically equivalent type doesn't change this fact.
Comment #15 by wfunction — 2012-05-19T19:28:45Z
(In reply to comment #14) > In a more general context, there's nothing to say a "pointer to member" must be a distinct type. It depends on what you mean by "pointer to member". If you say a VARIABLE is a pointer to a member, then yes, it doesn't have to have a distinct type. If you say a TYPE is a pointer to a member, then (by definition), it is a distinct type. (Otherwise, how do you distinguish between a type that is a pointer-to-member vs. one that isn't?)
Comment #16 by smjg — 2012-05-20T06:45:56Z
(In reply to comment #15) > It depends on what you mean by "pointer to member". If it means there are no pointer-to-member _types_ in D, then that's what it should say. > If you say a VARIABLE is a pointer to a member, then yes, it doesn't have to > have a distinct type. > > If you say a TYPE is a pointer to a member, then (by definition), it is a > distinct type. (Otherwise, how do you distinguish between a type that is a > pointer-to-member vs. one that isn't?) You wouldn't. They'd be one and the same type. I don't see any reason for them to be distinguished. If OTOH you implement a pointer-to-member as an index into the vtbl, so that it calls the correct method implementation for the object's subclass, _then_ you need a distinct type for it.
Comment #17 by yebblies — 2012-10-28T13:47:38Z
*** This issue has been marked as a duplicate of issue 3720 ***