Thread overview
alias template parameters - opCall caller! example breaks in Programming in D, page 530
7 hours ago
Brother Bill
6 hours ago
Nick Treleaven
6 hours ago
Nick Treleaven
5 hours ago
Brother Bill
4 hours ago
monkyyy
7 hours ago

On page 530 of Programming in D, we have this code snippet:

 class C {
 void opCall() {
 writeln("C.opCall called.");
 }
 }
 // ...
 auto o = new C();
 caller!o();
 caller!({ writeln("Function literal called."); })();

Converted this to source/app.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.");
	}
}

Compiled with DMD yielding error messages:

c:\dev\D\71 - 80\c78_3k_alias_template_opCall_overload\source\app.d(5): Error: template instance `caller!o` template `caller` is not defined
    caller!o();
    ^
c:\dev\D\71 - 80\c78_3k_alias_template_opCall_overload\source\app.d(7): Error: template instance `caller!(()
{
writeln("Function literal called.");
}
)` template `caller` is not defined
    caller!({ writeln("Function literal called."); })();
    ^

What is needed to get this code to compile?
My only idea is that a #import statement is missing.

6 hours ago

On Saturday, 23 August 2025 at 12:50:40 UTC, Brother Bill wrote:

>

On page 530 of Programming in D, we have this code snippet:

What section of the book?

>

Compiled with DMD yielding error messages:

c:\dev\D\71 - 80\c78_3k_alias_template_opCall_overload\source\app.d(5): Error: template instance `caller!o` template `caller` is not defined
    caller!o();
    ^
c:\dev\D\71 - 80\c78_3k_alias_template_opCall_overload\source\app.d(7): Error: template instance `caller!(()
{
writeln("Function literal called.");
}
)` template `caller` is not defined
    caller!({ writeln("Function literal called."); })();
    ^

What is needed to get this code to compile?
My only idea is that a #import statement is missing.

I'm not aware of any template function called caller - perhaps it is defined in the book somewhere?

6 hours ago

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?

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

https://dlang.org/book/templates_more.html#ix_More%20Templates.alias,%20template%20parameter

5 hours ago

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?

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.
4 hours ago

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?

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

https://dlang.org/book/templates_more.html#ix_More%20Templates.alias,%20template%20parameter

why is that education material not showing the fundamental pattern of (alias F,T...)(T args)=>F(args)?