Following code generates an AssertError when executed.
class forward(T)
{
void opDispatch(string s, T2)(T2 i)
{
import std.stdio;
writeln(s);
}
}
void main()
{
forward!(string) a;
a.foo(1);
}
dmd forward.d
./forward
[email protected](4): null this
----------------
./forward(_d_assert_msg+0x18) [0x807beb8]
./forward(void forward.forward!(immutable(char)[]).forward.opDispatch!("foo", int).opDispatch(int)+0x29) [0x8073c4d]
./forward(_Dmain+0xc) [0x8073c20]
./forward(extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).void runMain()+0x10) [0x807c3e0]
./forward(extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).void tryExec(scope void delegate())+0x18) [0x807c080]
./forward(extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).void runAll()+0x37) [0x807c427]
./forward(extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).void tryExec(scope void delegate())+0x18) [0x807c080]
./forward(_d_run_main+0x121) [0x807c051]
./forward(main+0x14) [0x807bf24]
/lib32/libc.so.6(__libc_start_main+0xe7) [0xf751b597]
./forward() [0x8073b41]
----------------
Comment #1 by k.hara.pg — 2014-08-26T07:14:50Z
(In reply to stevencoenen from comment #0)
> Following code generates an AssertError when executed.
>
> class forward(T)
> {
> void opDispatch(string s, T2)(T2 i)
> {
> import std.stdio;
> writeln(s);
> }
> }
>
> void main()
> {
> forward!(string) a;
> a.foo(1);
> }
forward!(string) is a class type, so the variable 'a' in main is initialized to null by default. Then the class member function 'foo' is invoked on null class reference.
You have to initialize the variable 'a' by using allocated class object.
void main()
{
forward!(string) a = new forward!(string)();
a.foo(1); // works
}