digitalmars.D - Why Templates?
- Derek Parnell (11/11) Mar 04 2006 I'm sure its because I'm not from a C++ background, but I just don't get...
- Sean Kelly (28/38) Mar 04 2006 That was the original intent. As far as I know, the original idea came
- Walter Bright (6/24) Mar 04 2006 Yes.
- Sean Kelly (4/34) Mar 04 2006 Very nice :-) Yet more to look forward to!
- Hasan Aljudy (9/21) Mar 04 2006 I feel the same.
- Ivan Senji (3/14) Mar 05 2006 It is the other way around, languages introduce common base class to
- Hasan Aljudy (3/21) Mar 05 2006 No. The Object base class fits perfectly into the object oriented
- Lars Ivar Igesund (2/25) Mar 05 2006 Luckily there is much more to programming than just OOP. :)
- Ivan Senji (6/24) Mar 05 2006 Maybe so, but I see a common Object base class a hack to solve many
- Walter Bright (6/10) Mar 05 2006 Technically, Java and C# implement generics, not templates. C++ and D
- Hasan Aljudy (2/18) Mar 05 2006 Are you saying that all generics do is silently insert casts?
- James Dunne (7/27) Mar 05 2006 I think they'd have to be, considering Java and C# are both reliant upon...
- Bruno Medeiros (9/27) Mar 05 2006 For all interested, here's an article that explains the differences in
- Ivan Senji (4/19) Mar 05 2006 I knew that C# templates/generics are different from C++/D ones but
- John C (6/16) Mar 05 2006 I'm sure you're wrong about C#'s implementation using casts. Java generi...
- Walter Bright (5/16) Mar 05 2006 From what I read, C# generics do not instantiate a new routine for every...
- Don Clugston (11/28) Mar 06 2006 It looks as though it has an optimisation for built-in types.
- John C (10/38) Mar 06 2006 I just wanted to put the record straight. For reference types, the same ...
- Matthias Spycher (7/9) Mar 06 2006 Don't forget that a dynamic compiler can do anything at runtime if the
- Andrew Fedoniouk (4/7) Mar 06 2006 Yep.
- Mark T (14/18) Mar 08 2006 sorry "generics" is a generic term, let's let not pigeon-hole it, Ada us...
- Georg Wrede (11/20) Mar 09 2006 Put like that, it becomes obvious how much of the generic stuff can be
- Tom (11/19) Mar 04 2006 Isn't this issue enough to justify templates?
- Sebastián E. Peyrott (12/12) Mar 04 2006 Well, I have no real experience with templates, either, but I do think t...
I'm sure its because I'm not from a C++ background, but I just don't get templates. What is the problem that Templates solve? From my limited experience they just seem to be a shorthand for a way of avoiding coding identical algorithms that only vary in the datatypes that are passed to them. Is that it or am I missing some fundamental elements? And is that why people are clamoring for implicit template instantation - to further reduce the amount of typing to do (and remembering to instantiate the only the required renditions of the algorithm). -- Derek Parnell Melbourne, Australia
Mar 04 2006
Derek Parnell wrote:I'm sure its because I'm not from a C++ background, but I just don't get templates. What is the problem that Templates solve? From my limited experience they just seem to be a shorthand for a way of avoiding coding identical algorithms that only vary in the datatypes that are passed to them. Is that it or am I missing some fundamental elements?That was the original intent. As far as I know, the original idea came from Alexander Stepanov (http://www.stepanovpapers.com/), who is also responsible for generics in Ada. But since their introduction, templates have also shown to be a useful high-performance alternative to run-time polymorphism, not to mention the more advanced code-generation trickery that has become so popular in the C++ world.And is that why people are clamoring for implicit template instantation - to further reduce the amount of typing to do (and remembering to instantiate the only the required renditions of the algorithm).For the most part, yes, as template names can become quite long, and calling functions as: fn!(typeof(a),typeof(b))(a,b); is a bit annoying and reduces readability. ITI is also quite useful in C++ with respect to how overload resolution is handled, but I have no idea how this will translate to the far simpler overloading rules in D. I think we really may have to simply want and see what Walter comes up with for ITI in D, as simply eliminating the need to explicitly specify template parameters doesn't encompass the full utility of ITI in C++. Will we be able to overload template functions with each other? Will they also overload with non-template functions? What happens if there are multiple matches? class C(T) {} template func(T) { void func( T val ) {} } template func(T) { void func( C!(T) val ) {} } void func( C!(int) val ) {} func( new C!(int) ); In C++, this is legal and the result is clearly defined (the "most specialized" overload is called), but how will this translate to D? Will it even be legal? Sean
Mar 04 2006
"Sean Kelly" <sean f4.ca> wrote in message news:duda9o$137k$1 digitaldaemon.com...is a bit annoying and reduces readability. ITI is also quite useful in C++ with respect to how overload resolution is handled, but I have no idea how this will translate to the far simpler overloading rules in D. I think we really may have to simply want and see what Walter comes up with for ITI in D, as simply eliminating the need to explicitly specify template parameters doesn't encompass the full utility of ITI in C++. Will we be able to overload template functions with each other?Yes.Will they also overload with non-template functions?No. This is a misfeature in C++, and the equivalent can be done with explicitly specialized template functions.What happens if there are multiple matches? class C(T) {} template func(T) { void func( T val ) {} } template func(T) { void func( C!(T) val ) {} } void func( C!(int) val ) {} func( new C!(int) ); In C++, this is legal and the result is clearly defined (the "most specialized" overload is called), but how will this translate to D?It'll be the most specialized.Will it even be legal? Sean
Mar 04 2006
Walter Bright wrote:"Sean Kelly" <sean f4.ca> wrote in message news:duda9o$137k$1 digitaldaemon.com...Cool. I feel pretty much the same.is a bit annoying and reduces readability. ITI is also quite useful in C++ with respect to how overload resolution is handled, but I have no idea how this will translate to the far simpler overloading rules in D. I think we really may have to simply want and see what Walter comes up with for ITI in D, as simply eliminating the need to explicitly specify template parameters doesn't encompass the full utility of ITI in C++. Will we be able to overload template functions with each other?Yes.Will they also overload with non-template functions?No. This is a misfeature in C++, and the equivalent can be done with explicitly specialized template functions.Very nice :-) Yet more to look forward to! SeanWhat happens if there are multiple matches? class C(T) {} template func(T) { void func( T val ) {} } template func(T) { void func( C!(T) val ) {} } void func( C!(int) val ) {} func( new C!(int) ); In C++, this is legal and the result is clearly defined (the "most specialized" overload is called), but how will this translate to D?It'll be the most specialized.
Mar 04 2006
Derek Parnell wrote:I'm sure its because I'm not from a C++ background, but I just don't get templates. What is the problem that Templates solve? From my limited experience they just seem to be a shorthand for a way of avoiding coding identical algorithms that only vary in the datatypes that are passed to them. Is that it or am I missing some fundamental elements? And is that why people are clamoring for implicit template instantation - to further reduce the amount of typing to do (and remembering to instantiate the only the required renditions of the algorithm).I feel the same. I do come from C++ background, but I just hate templates! (Now I hate C++ itself :) I think templates were introduced in C++ to solve the "no-base-class" problem. In other words, it's a hack that solves a misfeature in C++. D's templates are not really templates any more, they're more like a meta-programming facility. Or so I think, anyway. I don't really understand templates anyway, not to mention meta programming
Mar 04 2006
Hasan Aljudy wrote:I feel the same. I do come from C++ background, but I just hate templates! (Now I hate C++ itself :) I think templates were introduced in C++ to solve the "no-base-class" problem.It is the other way around, languages introduce common base class to solve the no-templates-problem. (Not a very good solution IMO).In other words, it's a hack that solves a misfeature in C++. D's templates are not really templates any more, they're more like a meta-programming facility. Or so I think, anyway. I don't really understand templates anyway, not to mention meta programming
Mar 05 2006
Ivan Senji wrote:Hasan Aljudy wrote:No. The Object base class fits perfectly into the object oriented paradigm. Templates do not, hence a hack.I feel the same. I do come from C++ background, but I just hate templates! (Now I hate C++ itself :) I think templates were introduced in C++ to solve the "no-base-class" problem.It is the other way around, languages introduce common base class to solve the no-templates-problem. (Not a very good solution IMO).In other words, it's a hack that solves a misfeature in C++. D's templates are not really templates any more, they're more like a meta-programming facility. Or so I think, anyway. I don't really understand templates anyway, not to mention meta programming
Mar 05 2006
Hasan Aljudy wrote:Ivan Senji wrote:Luckily there is much more to programming than just OOP. :)Hasan Aljudy wrote:No. The Object base class fits perfectly into the object oriented paradigm. Templates do not, hence a hack.I feel the same. I do come from C++ background, but I just hate templates! (Now I hate C++ itself :) I think templates were introduced in C++ to solve the "no-base-class" problem.It is the other way around, languages introduce common base class to solve the no-templates-problem. (Not a very good solution IMO).In other words, it's a hack that solves a misfeature in C++. D's templates are not really templates any more, they're more like a meta-programming facility. Or so I think, anyway. I don't really understand templates anyway, not to mention meta programming
Mar 05 2006
Hasan Aljudy wrote:Ivan Senji wrote:Maybe so, but I see a common Object base class a hack to solve many problems that are simple and efficient to solve with templates. For example containers. Containers in Java(before templates)-style and And one more thing <insert what Lars replyed> :)Hasan Aljudy wrote:No. The Object base class fits perfectly into the object oriented paradigm. Templates do not, hence a hack.I feel the same. I do come from C++ background, but I just hate templates! (Now I hate C++ itself :) I think templates were introduced in C++ to solve the "no-base-class" problem.It is the other way around, languages introduce common base class to solve the no-templates-problem. (Not a very good solution IMO).
Mar 05 2006
"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:duf72h$f40$1 digitaldaemon.com...Maybe so, but I see a common Object base class a hack to solve many problems that are simple and efficient to solve with templates. For example containers. Containers in Java(before templates)-style andimplement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.
Mar 05 2006
Walter Bright wrote:"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:duf72h$f40$1 digitaldaemon.com...Are you saying that all generics do is silently insert casts?Maybe so, but I see a common Object base class a hack to solve many problems that are simple and efficient to solve with templates. For example containers. Containers in Java(before templates)-style andimplement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.
Mar 05 2006
Hasan Aljudy wrote:Walter Bright wrote:serialization of code into JARs and assemblies. How would you serialize a template? -- Regards, James Dunne"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:duf72h$f40$1 digitaldaemon.com...Are you saying that all generics do is silently insert casts?Maybe so, but I see a common Object base class a hack to solve many problems that are simple and efficient to solve with templates. For example containers. Containers in Java(before templates)-style andimplement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.
Mar 05 2006
Hasan Aljudy wrote:Walter Bright wrote:For all interested, here's an article that explains the differences in more detail: http://www-128.ibm.com/developerworks/library/j-jtp01255.html This was posted in the NG by someone else that I don't recall, some time ago. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:duf72h$f40$1 digitaldaemon.com...Are you saying that all generics do is silently insert casts?Maybe so, but I see a common Object base class a hack to solve many problems that are simple and efficient to solve with templates. For example containers. Containers in Java(before templates)-style andimplement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.
Mar 05 2006
Walter Bright wrote:"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:duf72h$f40$1 digitaldaemon.com...as templates? That sucks.Maybe so, but I see a common Object base class a hack to solve many problems that are simple and efficient to solve with templates. For example containers. Containers in Java(before templates)-style andimplement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.
Mar 05 2006
"Walter Bright" <newshound digitalmars.com> wrote in message news:dufcpn$p1n$2 digitaldaemon.com..."Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:duf72h$f40$1 digitaldaemon.com...do erase the type information and cast everything to an object. But the CLR was reworked to support generics so List<int> remains a list of ints until it's instantiated at runtime.Maybe so, but I see a common Object base class a hack to solve many problems that are simple and efficient to solve with templates. For example containers. Containers in Java(before templates)-style andimplement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.
Mar 05 2006
"John C" <johnch_atms hotmail.com> wrote in message news:dufl8l$14au$1 digitaldaemon.com..."Walter Bright" <newshound digitalmars.com> wrote in message news:dufcpn$p1n$2 digitaldaemon.com...of argument types, it implements *one* routine which handles them all via casting.implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.do erase the type information and cast everything to an object. But the CLR was reworked to support generics so List<int> remains a list of ints until it's instantiated at runtime.
Mar 05 2006
Walter Bright wrote:"John C" <johnch_atms hotmail.com> wrote in message news:dufl8l$14au$1 digitaldaemon.com...It looks as though it has an optimisation for built-in types. http://www.artima.com/intv/generics2.html 100% syntactic sugar. There's a valid point raised there about the cryptic error messages generated in C++ templates. Currently D error messages for templates are even worse, but this is mainly because it keeps tying to instantiate templates after one has failed (and you can't even press ctrl-c to stop the torrent, which can last for over a minute). This is another area where D could give a great improvement over C++."Walter Bright" <newshound digitalmars.com> wrote in message news:dufcpn$p1n$2 digitaldaemon.com...of argument types, it implements *one* routine which handles them all via casting.implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.do erase the type information and cast everything to an object. But the CLR was reworked to support generics so List<int> remains a list of ints until it's instantiated at runtime.
Mar 06 2006
"Don Clugston" <dac nospam.com.au> wrote in message news:dugufa$2udq$1 digitaldaemon.com...Walter Bright wrote:I just wanted to put the record straight. For reference types, the same code is shared, but full type information is maintained, and there's no Object-to-type cast. With value types and structs, separate sections of code are created to explicitly avoid boxing/unboxing."John C" <johnch_atms hotmail.com> wrote in message news:dufl8l$14au$1 digitaldaemon.com..."Walter Bright" <newshound digitalmars.com> wrote in message news:dufcpn$p1n$2 digitaldaemon.com...set of argument types, it implements *one* routine which handles them all via casting.implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.generics do erase the type information and cast everything to an object. But the CLR was reworked to support generics so List<int> remains a list of ints until it's instantiated at runtime.It looks as though it has an optimisation for built-in types. http://www.artima.com/intv/generics2.htmlThere's a more technical overview here: http://research.microsoft.com/projects/clrgen/generics.pdf100% syntactic sugar. There's a valid point raised there about the cryptic error messages generated in C++ templates. Currently D error messages for templates are even worse, but this is mainly because it keeps tying to instantiate templates after one has failed (and you can't even press ctrl-c to stop the torrent, which can last for over a minute). This is another area where D could give a great improvement over C++.Yes, template error messages are notoriously difficult to decipher. Compilers need to address this.
Mar 06 2006
Don Clugston wrote:100% syntactic sugar.Don't forget that a dynamic compiler can do anything at runtime if the byte-code carries forward enough info about the original source -- class files are extensible. I have seen anonymous local classes reduced to simple functions. With decent escape analysis, allocation on the stack is possible for small value classes. Research on this topic, IMHO, has only scratched the surface.
Mar 06 2006
set of argument types, it implements *one* routine which handles them all via casting.Yep. And more here: Templates and Generics http://blogs.msdn.com/branbray/archive/2003/11/19/51023.aspx
Mar 06 2006
In article <dufcpn$p1n$2 digitaldaemon.com>, Walter Bright says...implement templates, not generics. What's the difference? One way to think about it is generics are handled at run time (by casting), templates at compile time.sorry "generics" is a generic term, let's let not pigeon-hole it, Ada uses the term and it is compile time the term "template" was originally associated with patterns, "In computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. The programming style emphasizing use of this technique is called generic programming." en.wikipedia.org/wiki/Template_(programming) "A generic term is one which picks out a class of individuals, or the prototype of the individual, rather than the individual itself. For example, in the sentence `The wolf has disappeared from northern Europe,' we are referring to the genus rather than to a particular wolf." www.informatics.susx.ac.uk/books/computers-and-thought/gloss/node1.html
Mar 08 2006
Mark T wrote:[T]he term "template" was originally associated with patterns, "In computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. The programming style emphasizing use of this technique is called generic programming." en.wikipedia.org/wiki/Template_(programming) [...]"...www.informatics.susx.ac.uk/books/computers-and-thought/gloss/node1.htmlPut like that, it becomes obvious how much of the generic stuff can be done with Interfaces. Even with the STL, all the stuff that trickles through "my custom made procedures and functions" needs certain properties. C++ did a good job of integrating the fundamental data types and structures to this. With anything that's objects, D can already be very powerful when one combines well factored interfaces with a solid set of orthogonal (not necessarily member-) functions. I love D!
Mar 09 2006
In article <op.s5wxef016b8z09 ginger.vic.bigpond.net.au>, Derek Parnell says...I'm sure its because I'm not from a C++ background, but I just don't get templates. What is the problem that Templates solve?I'm passing away... my pressure!!! :PFrom my limited experience they just seem to be a shorthand for a way of avoiding coding identical algorithms that only vary in the datatypes that are passed to them. Is that it or am I missing some fundamental elements?Isn't this issue enough to justify templates? I have some interesting experience with C++, I'm not an expert even though I've coded a lot in it and I've used "nearly all" (that means excluding the infamous ones such as those modern cast operators :), etc.). I have no doubts, I can say that one of the best features I've ever seen working in a language is the C++ templates. I've coded really generic functions/containers with a great success in C++ and used them with a variety of stuff. Always worked nice!And is that why people are clamoring for implicit template instantation - to further reduce the amount of typing to do (and remembering to instantiate the only the required renditions of the algorithm).Tom;
Mar 04 2006
Well, I have no real experience with templates, either, but I do think there are many interesting (and useful) coding techniques that require them. And even though, I, for the most part, and as Derek, see them as a way of coding similar algorithms based on different data-types, IMO the most important characteristic is compile-time code generation. When to use it, and how to take advantage of that feature, be it to create abstractions of similar algorithms based on differing data-types, or not, is a whole other issue. I'm not entirely sure whether there are other ways of achieving the same thing, so templates, for the most part, seem like an important feature for a language such as D. -- Sebastián.
Mar 04 2006