On Sunday, 2 March 2014 at 11:47:39 UTC, Steve Teale wrote:
On Sunday, 2 March 2014 at 10:05:05 UTC, Dicebot wrote:


There is nothing wrong about not using templates. Almost any compile-time design can be moved to run-time and expressed in more common OOP form. And using tool you have mastery of is usually more beneficial in practice than following the hype.

Yes DB, we can soldier on happily, but it would not do any harm to understand templates.

The documentation examples quickly make your eyes glaze over, looking at the code in Phobos is doubtless instructive, but you can wade through a lot of that without finding what you want. Also I discovered an interesting fact today. the word 'mixin' does not appear in the language reference Templates section of dlang.org.

It should be used in at least one example. I just discovered by trial and error that I could use 'mixin' in Templates (as opposed to Template Mixins), and when you know that it seems likely that you can accomplish lots of stuff you couldn't before.

While I'm here, has anyone discovered a way to fudge a constructor super(..) call in a mixin template that's included in a class constructor. Since the mixin template is evaluated in the scope of the constructor, it seems like it should be OK.

I'm sure I'll get there in time ;=)

Steve

You've got to learn to think a bit more abstractly. Templates are generalizations of things.

Suppose I want to add two numbers using a function.

int add(int, int)?
double add(double, int)?
float add(float, int)?
char add(char, double)?
etc....

which one? Do you want to have to create a function every time for every time?

Whats the only significant difference between all of them? If you can't answer this then you can't abstract and which is the reason you don't understand templates.


I could use one template function to handle all those cases above. That's what makes it powerful. I can basically write one function when you have to write 8, 10, 20 or whatever.

S add(S, T)(S a, T b) { return cast(S)(a + b); }

The compiler then generates all the concrete use cases for me. (and using oop can make it more powerful, S and T could be vectors) As long as the first type has an binary addition operator on it that takes the second type it will work and the template will work without change.

But you want to continue using the hold way. It is analogous to those that want to continue to write procedural code because they don't see the what oop has to offer.

BTW, how did you learn oop? Did you understand it all perfectly by reading a book or did you learn best by writing code that used it?

If you don't attempt to use templates, even in example code, you won't get it.



Reply via email to