Tips for golfing in D2
Function calls
// use
i.writeln;
// instead of
writeln(i);
// use
writeln;
// instead of
writeln();
Conditions
Use the ternary operator (?:
) or &&
/||
:
if(i>0)i.write;
if(i)i.write;
i?i.write:0;
i&&i.write;
auto
keyword
string s="foo";
auto s="foo";
Terminating by error
If your program terminates by error, you can replace void main
with int main
. For example, the shortest way to loop over args:
import std;int main(string[]a){for(int i;;)a[++i].writeln;}
main parameters
The compiler may have told you,
function D main
parameter list must be empty or accept one parameter of type string[]
but actually, the main function can accept a char[][]
parameter instead. This can allow you to use split
on an argument instead of splitter
.