Why not move cast to the standard library?

bearophile bearophileHUGS at lycos.com
Fri Sep 25 17:35:34 PDT 2009


Adam D. Ruppe:

> macro max(int a, int b) {
> 	return a ~ " > " ~ b ~ " ? " ~ a ~ " : " ~ b;
> }

In std.metastrings there's Format that allows to use a basic form of string templating, that I find a little more readable than many string concats:

return a ~ " > " ~ b ~ " ? " ~ a ~ " : " ~ b;
==>
Format!("return %s > %s ? %s : %s;", a, b, a, b);

Or safer:
Format!("return %d > %d ? %d : %d;", a, b, a, b);

Or more readable, something like:
return "{a} > {b} ? {a} : {b};" % (a, b, a, b);

Bye,
bearophile



More information about the Digitalmars-d mailing list