alias template parameters - opCall caller! example breaks in Programming in D, page 530

Brother Bill brotherbill at mail.com
Sat Aug 23 14:46:38 UTC 2025


On Saturday, 23 August 2025 at 13:42:52 UTC, Nick Treleaven wrote:
> On Saturday, 23 August 2025 at 13:38:27 UTC, Nick Treleaven 
> wrote:
>> I'm not aware of any template function called `caller` - 
>> perhaps it is defined in the book somewhere?
>
> ```d
> void caller(alias func)() {
>     write("calling: ");
>     func();
> }
> ```
> https://dlang.org/book/templates_more.html#ix_More%20Templates.alias,%20template%20parameter

That is the missing piece.

app/source.d
```
import std.stdio;

void main() {
	auto o = new C();
	caller!o();

	caller!({ writeln("Function literal called."); })();
}

class C {
	void opCall() {
		writeln("C.opCall called.");
	}
}

void caller(alias func)() {
     write("calling: ");
     func();
}

```

Running produces: (matching the book)
```
calling: C.opCall called.
calling: Function literal called.
```


More information about the Digitalmars-d-learn mailing list