Question

Template members may be implicitly referred to if they are the only member of a template, and if they share the template's name:

template foo(int number)
{
    immutable int foo = number;
}

void main()
{
    writeln(foo!(123)); // Okay.
}

But what if I want to refer to the member explicitly?

writeln(foo!(123).foo); // Error: attempts to access the foo property of int.

I have no good reason for this, but I feel like it must be possible.

Was it helpful?

Solution

Eponymous templates are replaced with their values when used. So, as far as the compiler is concerned, writing

writeln(foo!(123).foo);

is basically the same as writing

writeln(123.foo);

And that's not legal. That line would result in basically the same error that you're getting. You're not supposed to access the members of an eponymous template. They are intentionally opaque.

OTHER TIPS

You can't -- eponymous templates are opaque; you can't get at them like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top