PhilippeSigaud/D-templates-tutorial — Issue #6: Example on page 13 not working
Author: jacob-carlborg
Created: 2012-01-14 14:32:03+00:00
State: open

The second example on page 13 doesn't work.

template NameOf (alias name)
{
enum string NameOf = name.stringof;
}

int foo (int i) { return i + 1; }

auto s1 = NameOf!(foo); // this will fail
auto s2 = NameOf!(NameOf);

Compiling the code will result in an error similar to this:

test.d(8): Error: function test.foo (int i) is not callable using argument types ()
test.d(8): Error: expected 1 function arguments, not 0
test.d(15): Error: template instance test.NameOf!(foo) error instantiating

The reason is because the compiler thinks you're trying to call "foo". D allows to call a function taking no arguments without parentheses, that's how properties are implemented.