Should I stop being interested in D language if I don't like to see template instantiation in my code?
Steven Schveighoffer
schveiguy at gmail.com
Wed Nov 13 16:17:02 UTC 2019
On 11/13/19 9:01 AM, BoQsc wrote:
> I don't like to see exclamation marks in my code in as weird syntax as
> these ones:
>> to!ushort(args[1])
>> s.formattedRead!"%s!%s:%s"(a, b, c);
>
> I'm not sure why, but template instantiation syntax is prevalent in the
> documentation examples of d lang libraries. It almost seems like every
> other example contains at least one or two of them.
>
> It look horrible, and I'm feeling like I'm being forced/coerced to learn
> from examples that do not provide alternatives to the template
> instantiation syntax. Even if the alternative examples were provided,
> why would anyone want to have syntax as ugly and weird as current
> template instantiation syntax with exclamation point in the middle of
> the statement with all other things that come with it.
>
I really encourage you to learn to like templates. They are awesome.
But D of course provides so many tools, you can almost get away with not
seeing them instantiated.
struct To
{
import std.conv : to;
static opDispatch(string s, Args...)(Args args) {
static if(s.startsWith("_")) // needed for keywords
return opDispatch!(s[1 .. $])(args);
else
mixin("return to!" ~ s ~ "(args);");
}
}
To._ushort(args[1]); -> translates to to!ushort(args[1]).
But that's a lot of work. Better to just use the library as is. People
will understand it more.
Note that none other than Andrei himself once proposed changing template
instantiation syntax (to . instead of !)
-Steve
More information about the Digitalmars-d-learn
mailing list