April 25, 2015
struct MyStruct {}

void main(string[] args) {
	string str = "blah-blah";

	auto d1 = (MyStruct) { writeln("delegate-str: ", str); };

	writeln(typeid(typeof(d1)));
}



dmd: 2067
os: Win8.1
build script: dmd main.d -ofconsole-app.exe -debug -unittest -wi

- if delegate has no params or param is declared as int/char/float then the code compiles successfully.
	auto d1 = (int) { writeln("delegate-str: ", str); };

- if I declare named param as string or MyStruct then the code compiles successfully too.
	auto d1 = (MyStruct ms)  { writeln("delegate-str: ", str); };

- if I declare anonymous parameter as string or MyStruct then the error compile occurs:
	auto d1 = (MyStruct)  { writeln("delegate-str: ", str); };

main.d(21): Error: variable main.main.d1 type void is inferred from initializer (MyStruct)

{

writeln("delegate-str: ", str);

}

, and variables cannot be of type void
main.d(21): Error: template lambda has no value

Why does it happen?
April 25, 2015
On Saturday, 25 April 2015 at 10:23:25 UTC, ref2401 wrote:
> struct MyStruct {}
>
> void main(string[] args) {
> 	string str = "blah-blah";
>
> 	auto d1 = (MyStruct) { writeln("delegate-str: ", str); };
>
> 	writeln(typeid(typeof(d1)));
> }
>
>
>
> dmd: 2067
> os: Win8.1
> build script: dmd main.d -ofconsole-app.exe -debug -unittest -wi
>
> - if delegate has no params or param is declared as int/char/float then the code compiles successfully.
> 	auto d1 = (int) { writeln("delegate-str: ", str); };

This seems to be a special case for builtin types. Builtin types cannot be used for identifiers, so "int" is recognized as the type here.

> - if I declare named param as string or MyStruct then the code compiles successfully too.
> 	auto d1 = (MyStruct ms)  { writeln("delegate-str: ", str); };

Here you give both type and name, so it's clear which is which, to both you and the compiler.

> - if I declare anonymous parameter as string or MyStruct then the error compile occurs:
> 	auto d1 = (MyStruct)  { writeln("delegate-str: ", str); };
>
> main.d(21): Error: variable main.main.d1 type void is inferred from initializer (MyStruct)
>
> {
>
> writeln("delegate-str: ", str);
>
> }
>
> , and variables cannot be of type void
> main.d(21): Error: template lambda has no value

Here "MyStruct" is interpreted as the _name_ of the parameter. Its type is inferred as void, because there's no context that says otherwise. Parameters cannot be typed void -> error.

Generally, in function literals, a single identifier is interpreted as the name, because the type can often be inferred from context. And then it's nice not having to write the type out.

> Why does it happen?