Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
November 13, 2019 Should I stop being interested in D language if I don't like to see template instantiation in my code? | ||||
---|---|---|---|---|
| ||||
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.
|
November 13, 2019 Re: Should I stop being interested in D language if I don't like to see template instantiation in my code? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | On Wednesday, 13 November 2019 at 14:01:13 UTC, 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'd suggest learning to love it, or at least getting to it, vs. to<ushort>(args[1]) C++ syntax. Template *syntax* benefits are 1. D's is relatively quick to parse. 2. the ! is an attention getter so you can see that some special action is taking place (although templates don't always require this). Personally this is more of a comfort ("I'm *definitely* doing some work at compile-time here, instead of at runtime like other languages force me to") than an alert ("something weird is happening!") though. 3. it's very clear what's a compile-time vs. a runtime parameter to the template. > 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. Templates are so beneficial that people put up with C++. D uses them everywhere and if you persist in hating the syntax you'll just hate a lot of the D syntax that you see. |
November 13, 2019 Re: Should I stop being interested in D language if I don't like to see template instantiation in my code? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | On Wednesday, November 13, 2019 7:01:13 AM MST BoQsc via Digitalmars-d-learn 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.
D uses !() rather than <>, because it's not ambiguous, allowing for the parser to be context-free (whereas a language like C++ or Java has to actually process the context in order to know whether something like the >> in vector<Tuple<int, float>> is a template instantation or a shift operation; parsing is _much_ cleaner if it can be context-free). D then allows the parens to be dropped when there's only one template argument, which is why you get stuff like s.formattedRead!"" instead of s.formattedRead!("").
Regardless of the syntax though, like C++'s standard library, D's standard library uses templates quite a bit, and it's extremely common for D code in general to use templates. I don't know why you think that using an exclamation point for template instantiations is ugly, but if you can't stand it, you're not going to be happy with D, because you're going to see it quite a lot in typical D code.
- Jonathan M Davis
|
November 13, 2019 Re: Should I stop being interested in D language if I don't like to see template instantiation in my code? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | On Wednesday, 13 November 2019 at 14:01:13 UTC, 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);
No pressure to use templates. D is designed to be multi-paradigm, and in many was combines the advantages of c++ and c# even if you use just traditional imperative or object oriented ways. For example, C# has array bounds checking, C++ has raw type casting, but D has both. And neither of the mentioned languages have `scope (exit`. Granted, some other new language, like Go, might be even better if you don't care about templates.
That being said, I think you definitely should learn about templates. First, it let's you understand code written by others, and secondly you can evaluate whether you really want to code without them. There are problems you simply cannot deal with as efficiently as you can with templates. For example, if you write a math function you want to work with both integers and floats, you basically have four options:
1: Code duplication.
2: Runtime casting of the argument to the type the function is implemented in, wasting computational power. Traditional object-oriented solution falls in this category.
3: Using a macro preprocessor. Macros are unaware of code semantics and thus are known for making codebases buggy and hard to maintain.
4: Templates. Same code size bloat as with options 1 and 3, but otherwise basically no downsides.
|
November 13, 2019 Re: Should I stop being interested in D language if I don't like to see template instantiation in my code? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dukc | On Wednesday, 13 November 2019 at 14:59:57 UTC, Dukc wrote:
> 4: Templates. Same code size bloat as with options 1 and 3,
Meant binary size bloat
|
November 13, 2019 Re: Should I stop being interested in D language if I don't like to see template instantiation in my code? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mipri | On Wednesday, 13 November 2019 at 14:23:40 UTC, mipri wrote: > 3. it's very clear what's a compile-time vs. a runtime parameter to the template. At least, if one understands the difference. I remember that I was not aware of this, when I started learning D and was a lot confused by the use of templates in Phobos. And maybe for BoQsc it's the same. Therefore: Take the second example: This could also be written as s.formattedRead("%s!%s:%s", a, b, c); Now you've got no ! anymore. The disadvantage of this is, that parsing "%s!%s:%s" happens at runtime, meaning it is done everytime, the function is executed. If there are lots of such reads, the code can get considerably slower. The alternative is s.formattedRead!("%s!%s:%s")(a, b, c); I put the parenthesis around "%s!%s:%s" to make it more clear, that this is also some sort of function argument, like a, b and c. But this time, the compiler allready parses "%s!%s:%s" at compiletime, which means, when running the program it allready knows, that there are no errors in that string and maybe (I don't know the details) also, which parameter to put where and so on. That's faster. :-) Believe me: It's worth to learn about templates! |
November 14, 2019 Re: Should I stop being interested in D language if I don't like to see template instantiation in my code? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | On Wednesday, 13 November 2019 at 14:01:13 UTC, 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. A good thing is that in many cases the template instance parameters can be deduced from the arguments used: --- import std; void main() { assert(max(0,1) == 1); // same as assert(max!(int,int)(0,1) == 1); } --- This feature is known as "IFTI" see §6, https://dlang.org/spec/template.html#function-templates. You're not forced to use the D templates but you'll have to write many code by yourself because the standard library use them everywhere. |
November 14, 2019 Re: Should I stop being interested in D language if I don't like to see template instantiation in my code? | ||||
---|---|---|---|---|
| ||||
Posted in reply to user9876 | On Thursday, 14 November 2019 at 09:30:23 UTC, user9876 wrote:
> A good thing is that in many cases the template instance parameters can be deduced from the arguments used:
>
> ---
> import std;
>
> void main()
> {
> assert(max(0,1) == 1);
> // same as assert(max!(int,int)(0,1) == 1);
> }
> ---
>
> This feature is known as "IFTI" see §6, https://dlang.org/spec/template.html#function-templates.
>
> You're not forced to use the D templates but you'll have to write many code by yourself because the standard library use them everywhere.
IFTI is nifty. (sorry, I had to)
--
Simen
|
Copyright © 1999-2021 by the D Language Foundation