www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DMD does not allow stringof on function alias of a function that

reply d coder <dlang.coder gmail.com> writes:
--e0cb4efe325433544a04c17bc7d8
Content-Type: text/plain; charset=ISO-8859-1

Greetings All

Using DMD 2.059, the following code does not compile. DMD does not allow me
to apply stringof on a function alias of a function that takes one or more
arguments. It compiles and runs fine for functions that do not take any
arguments.

Is it a bug, or am I doing something wrong here?

Regards
- Puneet

class Foo {
  void foo() { }
  void frop(int n) { }
}

void callfoo(alias F, T) (T t) {
  import std.stdio;
  writeln(F.stringof);
}

void main() {
  Foo f = new Foo();
  callfoo!(Foo.foo)(f); // This works
  callfoo!(Foo.frop)(f); // This does not work
}

--e0cb4efe325433544a04c17bc7d8
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>Greetings All</div><div><br></div><div>Using DMD 2.059, the following =
code does not compile. DMD does not allow me to apply stringof on a functio=
n alias of a function that takes one or more arguments. It compiles and run=
s fine for functions that do not take any arguments.</div>

<div><br></div><div>Is it a bug, or am I doing something wrong here?</div><=
div><br></div><div>Regards</div><div>- Puneet</div><div><br></div><div>clas=
s Foo {</div><div>=A0 void foo() { }</div><div>=A0 void frop(int n) { }</di=
v>

<div>}</div><div><br></div><div>void callfoo(alias F, T) (T t) {</div><div>=
=A0 import std.stdio;</div><div>=A0 writeln(F.stringof);</div><div>}</div><=
div><br></div><div>void main() {</div><div>=A0 Foo f =3D new Foo();</div><d=
iv>
=A0 callfoo!(Foo.foo)(f); // This works</div>
<div>=A0 callfoo!(Foo.frop)(f); // This does not work</div><div>}</div><div=
<br></div>

--e0cb4efe325433544a04c17bc7d8--
Jun 02 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-06-02 13:40, d coder wrote:
 Greetings All

 Using DMD 2.059, the following code does not compile. DMD does not allow
 me to apply stringof on a function alias of a function that takes one or
 more arguments. It compiles and runs fine for functions that do not take
 any arguments.

 Is it a bug, or am I doing something wrong here?

 Regards
 - Puneet

 class Foo {
    void foo() { }
    void frop(int n) { }
 }

 void callfoo(alias F, T) (T t) {
    import std.stdio;
    writeln(F.stringof);
 }

 void main() {
    Foo f = new Foo();
    callfoo!(Foo.foo)(f); // This works
    callfoo!(Foo.frop)(f); // This does not work
 }

I'm guessing that the problem is that you can call a function without parentheses in D (this is how properties are implemented). So the problem is that it will actually try and call the function with no arguments, which will fail since it expects, in this case, an int. Try taking the address of the function first: writeln(&F.stringof); -- /Jacob Carlborg
Jun 02 2012