Comment #0 by shammah.chancellor — 2015-03-28T16:29:35Z
DMD fails with:
test0104.d(12): Error: function test0104.T.add (int a) is not callable using argument types ()
```test0104.d
//T compiles:yes
//T has-passed:yes
//T retval:42
// Test creation of delegates from member function.
struct S {
int i;
T t;
auto add(int a) {
t.i = a + i;
return t.add;
}
}
struct T {
int i;
int add(int a) {
return i + a;
}
}
int main() {
S s;
s.i = s.t.i = 1;
auto dg1 = s.add;
auto dg2 = dg1(34);
return dg2(7);
}
```
Comment #1 by ag0aep6g — 2015-03-28T16:46:35Z
I think this is invalid. `t.add` and `s.add` are parentheses-less calls. So the error about T.add not being callable "using argument types ()" is correct. To get a delegate, add '&':
struct S {
int i;
T t;
auto add(int a) {
t.i = a + i;
return &t.add; /* ! */
}
}
struct T {
int i;
int add(int a) {
return i + a;
}
}
int main() {
S s;
s.i = s.t.i = 1;
auto dg1 = &s.add; /* ! */
auto dg2 = dg1(34);
return dg2(7);
}
Comment #2 by shammah.chancellor — 2015-03-28T16:48:14Z
Definitely possible. I will file an SDC bug if this turns out to be the case.
Comment #3 by shammah.chancellor — 2015-03-28T18:18:22Z
I'm not so sure this is desirable. What if there add as a ref return. What does &t.foo give then?
Comment #4 by ketmar — 2015-03-28T18:25:16Z
i believe that it will be a bug in the D code, as there is no variable to ref. at least it should be a bug. ;-)
Comment #5 by ag0aep6g — 2015-03-28T19:04:23Z
(In reply to Shammah Chancellor from comment #3)
> I'm not so sure this is desirable. What if there add as a ref return. What
> does &t.foo give then?
You mean something like the following?
----
struct T
{
int i = 42;
ref int foo() {return i;}
}
void main()
{
T t;
auto dg = &t.foo;
auto i = &t.foo();
}
----
Here dg is a delegate of the method foo and i is a pointer to the ref-returned field i.
The spec says this [1]:
"In most places, getter property functions are called immediately. One exceptional case is the address operator."
"Even if the given operand is a property function, the address operator returns the address of the property function rather than the address of its return value."
Now foo is no @property, but the spec says that "[if] a function call does not take any arguments syntactically, it is callable without parenthesis, like a getter property functions." So it's reasonable that the same rules apply.
[1] http://dlang.org/function.html#property-functions
Comment #6 by k.hara.pg — 2015-03-29T09:10:42Z
(In reply to ag0aep6g from comment #1)
> I think this is invalid. `t.add` and `s.add` are parentheses-less calls. So
> the error about T.add not being callable "using argument types ()" is
> correct. To get a delegate, add '&':
That's right. When a function symbol identifier without address operator appears in expressions, it will be evaluated to a parenthesis-less function call.
Therefore, it's definitely a bug in SDC.