www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template expression

reply Pac <Pac_member pathlink.com> writes:
Hello,


does D support Templares expression. If yes where may I find some documentation
on   it ?


Best regards
Pac
Jul 18 2004
next sibling parent reply J C Calvarese <jcc7 cox.net> writes:
Pac wrote:
 Hello,
 
 
 does D support Templares expression. If yes where may I find some documentation
 on   it ?
 
 
 Best regards
 Pac

What do you mean by "Template expression"? Have you tried these links? http://www.digitalmars.com/d/template.html http://www.dsource.org/tutorials/index.php?show_topic=Templates -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Jul 18 2004
parent reply Pac <Pac_member pathlink.com> writes:
In article <cdfnd9$1sr$1 digitaldaemon.com>, J C Calvarese says...
Pac wrote:
 Hello,
 
 
 does D support Templares expression. If yes where may I find some documentation
 on   it ?
 
 
 Best regards
 Pac

What do you mean by "Template expression"? Have you tried these links?

of course. Short example of expression templates in C++ : http://www.cuj.com/documents/s=8232/cuj0303langer/ Best regards Pac
Jul 19 2004
parent reply Sean Kelly <sean f4.ca> writes:
Sort of.  D supports templates so such things are possible, but as D requires
explicit type specification this sort of thing is incredibly unwieldy.  There
has been some talk of optimized expression evaluation which may obviate the need
for expression templates, but it's on the "to do" list.


Sean
Jul 19 2004
parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
Sean Kelly wrote:

 Sort of.  D supports templates so such things are possible, but as D
 requires
 explicit type specification this sort of thing is incredibly unwieldy. 
 There has been some talk of optimized expression evaluation which may
 obviate the need for expression templates, but it's on the "to do" list.

What was that talk about? If you mean the recent discussion about vector expressions: yes, they would eliminate one of the most common uses of expression templates, but they would not be a replacement for expression templates in general. If there were an idea to really replace expression templates in general, I would be really interested in it - even though I doubt that it will be possible.
Jul 20 2004
parent Sean Kelly <sean f4.ca>s writes:
In article <cdinql$1a3u$1 digitaldaemon.com>, Norbert Nemec says...
Sean Kelly wrote:

 Sort of.  D supports templates so such things are possible, but as D
 requires
 explicit type specification this sort of thing is incredibly unwieldy. 
 There has been some talk of optimized expression evaluation which may
 obviate the need for expression templates, but it's on the "to do" list.

What was that talk about? If you mean the recent discussion about vector expressions: yes, they would eliminate one of the most common uses of expression templates, but they would not be a replacement for expression templates in general.

That's what I meant. And I agree that it might only be a partial replacement, though that is contingent on what the solution ends up being :)
If there were an idea to really replace expression templates in general, I
would be really interested in it - even though I doubt that it will be
possible.

Same here. But I think this is something that should probably be delayed until the core language features are pretty well established. I'm personally still hopeful that implicit template type determination may make it in at some point, though I suppose this would be a 2.0 feature as well. Sean
Jul 20 2004
prev sibling next sibling parent reply Stephen Waits <steve waits.net> writes:
Pac wrote:
 
 does D support Templares expression. If yes where may I find some documentation
 on   it ?

The thing about it is.. C++ template expressions are a nasty ugly hack. My question is, can we get equivalent performance with an increase in elegance an simplicity in D? The clear answer right now and at least through 1.0 is "NO". --Steve
Jul 19 2004
parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
Stephen Waits wrote:

 The thing about it is.. C++ template expressions are a nasty ugly hack.

This is not quite right. It is true, that meta-programming in C++ is a dirty business. But so far, I know of no other language (except a few experimental ones that never made it out into the real world) that offers a similar functionality. I agree, that in C++, the syntax and a few details are designed suboptimal, but I doubt that the core idea of expression templates can be captured by a simpler technique.
   My question is, can we get equivalent performance with an increase in
 elegance an simplicity in D?

I strongly doubt it. D templates are different from C++, but they share a key property: template parameters are untyped. Template instantiation is mostly a textual copy-and-paste operation done by the compiler. A template cannot be compiled on its own. (This is different to languages that offer "parameterized classes" which can be compiled without knowledge of the types used at instantiation.) We may have a cleaner syntax for templates in D, as well as a number of convenient features, but in principle, expression templates will either look similar or be less powerful than in C++. (And here I would prefer the first option!)
 The clear answer right now and at least through 1.0 is "NO".

In this, I agree.
Jul 20 2004
parent reply Stephen Waits <steve waits.net> writes:
Norbert Nemec wrote:
 Stephen Waits wrote:
 
The thing about it is.. C++ template expressions are a nasty ugly hack.

This is not quite right. It is true, that meta-programming in C++ is a dirty business. But so far, I know of no other language (except a few experimental ones that never made it out into the real world) that offers a similar functionality. I agree, that in C++, the syntax and a few details are designed suboptimal, but I doubt that the core idea of expression templates can be captured by a simpler technique.

I think I'm correct in saying that it's a "nasty ugly hack". However, I agree, the functionality has yet to be reproduced in a language other than C++. My idea is that, given what we've learned with C++ in the past decade, perhaps now we can achieve the same functionality, without it being a "nasty ugly hack". Think it's possible? Or am I overly optimistic?
 We may have a cleaner syntax for templates in D, as well as a number of
 convenient features, but in principle, expression templates will either
 look similar or be less powerful than in C++. (And here I would prefer the
 first option!)

I too will take performance, and functionality, over any syntax. In my work it's a major priority. --Steve
Jul 20 2004
parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
Stephen Waits wrote:
 Norbert Nemec wrote:
 Stephen Waits wrote:
 
The thing about it is.. C++ template expressions are a nasty ugly hack.

This is not quite right. It is true, that meta-programming in C++ is a dirty business. But so far, I know of no other language (except a few experimental ones that never made it out into the real world) that offers a similar functionality. I agree, that in C++, the syntax and a few details are designed suboptimal, but I doubt that the core idea of expression templates can be captured by a simpler technique.

I think I'm correct in saying that it's a "nasty ugly hack". However, I agree, the functionality has yet to be reproduced in a language other than C++. My idea is that, given what we've learned with C++ in the past decade, perhaps now we can achieve the same functionality, without it being a "nasty ugly hack". Think it's possible? Or am I overly optimistic?

First, we should try to identify what makes expression templates so ugly in C++. I think, the basic concept of them should be kept in D. It certainly is not a simple concept, but meta-programming just is an advanced subject, so one should not hope for a simple solution. The problem therefore are not the expression templates, but more fundamental: the meta-language in C++. Looking closely, this meta-language is a purely functional language which is executed at compile-time. It handles types and constant values. The "functions" of this language are the templates (taking types and constants as parameters and "returning" types and constants through the content that can be accessed from outside) The problem in C++ is this language, which was actually found by accident and which is ugly to program, ugly to debug and inefficient to execute (resulting in extremely long compile times for et-programs) What we can do in D is to try to improve and clean up this language. Most of it is already given by the mechanism of templates. The concept already is very similar to that of C++, so it is too late already for going completely different direction, but there is still a lot of room left for improvement. A few ideas: * allowing function calls in constant expressions (restricted to pure functions with only constant arguments) - the compiler would have to be able to identify pure functions (no side-effects, no access to anything besides the arguments) but that should be possible without changes to the language. * compile-time flow control - basically, a compile-time "if" extending the idea of "version", it should take any constant boolean argument, be allowed outside of functions and not open a new namespace. * string handling at compile time * identifier <-> string <-> integer conversion at compile-time * maybe most advanced: "list"-handling at compile time Anyhow, all of this is, of course far beyond 1.0 ...
Jul 21 2004
prev sibling parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
Hi there,

looking at your later comments, I find that you mean "expression templates".

Actually, these are not just one feature, but a collection of features in
C++ that work together allowing a certain way of programming.

Most of these features are present in D as well. Templates in D are
conceptually different from C++ templates, but they should allow mostly
everything that can be done in C++. Considering the complexity of the C++
template mechanism, it will probably need some more investigation to find
out whether D templates really capture all aspects, but I'm pretty
confident about this.

The only major point lacking for expression template programming, though, is
implicit instantiation of function templates. This has been a reason for
debate for some time now. It will most certainly not be part of D 1.0, but
I have little doubt that one day, Walter will give in to the fact that
without implicit instantiation, things like expression templates are just
impossible.

So, the short answer to your question: No, not at the moment, but hopefully,
yes, in the future.

Ciao,
Nobbi


Pac wrote:

 Hello,
 
 
 does D support Templares expression. If yes where may I find some
 documentation
 on   it ?
 
 
 Best regards
 Pac

Jul 20 2004