On 5/10/07, Bob Schmertz <[EMAIL PROTECTED]> wrote:
John Demme incurred the wrath of Bob on May 8, by saying
>This isn't really the purpose of templates in D...
>
>However, it can be done. The following program:
>
>import tango.io.Stdout;
>
>void showValue(alias D)() {
> Stdout(D.stringof)(" = ")(D).newline;
>}
>
>void main() {
> int x = 5;
> double d = 5.9;
>
> showValue!(x);
> showValue!(d);
>}
>
>prints:
>[EMAIL PROTECTED] ~ $ ./test
>x = 5
>d = 5.90
>
OK, this does pretty much what I wanted for this case, so this is good.
I wish I could say I understand it. Is there somewhere, on the site or
elsewhere, that gives a good explanation of aliases outside of template
use? Even in templates they're kind of tricky -- or unfamiliar, at
least.
http://digitalmars.com/d/declaration.html
Take a look at "Type Aliasing". alias outside of template parameters are
quite different from their use in template parameters, although at a
conceptual level, they are very similar. The concept is basically just to
take a symbol and make it available via a different name. The simple
example is:
class ThisClassHasSomeReallyLongJavaStyleName {}
alias ThisClassHasSomeReallyLongJavaStyleName ShortName;
But a similar thing happens in templates. Take the above example with debug
printing. Basically, the alias says "This template parameter represents an
arbitrary symbol, and we're going to refer to it as 'D' - do whatever you
want with it, and we'll check to see if you can really do that with it at
compile time." Most symbols (maybe all) in D have this .stringof property
which just returns the string representation of that symbol. Take a look
at:
http://digitalmars.com/d/property.html
So the example above is a templated function- the first set of () are the
template parameters, passed in at compile time, the second (empty) set are
the function parameters, passed at runtime- and D is just any symbol. In
this context, you can basically treat it as a C-macro-style text replace,
but it's actually much more complex and expressive (more powerful, IMO) than
that.
Clearer?
~John