describe-d: an introspection library
Jean-Louis Leroy
jl at leroy.nyc
Wed Apr 22 13:32:03 UTC 2020
On Wednesday, 22 April 2020 at 05:20:18 UTC, Stefan Koch wrote:
> On Tuesday, 21 April 2020 at 14:43:04 UTC, Jean-Louis Leroy
> wrote:
>
>
>> I wonder if templates are lazily expanded. I haven't looked at
>> the compiler's code, my guess is: maybe not.
>
> If the template gets used it gets instantiated (and cached).
I did not word my question precisely, I meant: is it instantiated
in its *entirety*.
Consider:
import std.meta;
template Function(Attributes...)
{
enum isPure = Attributes[0];
enum isNogc = Attributes[1];
alias parameters = Attributes[2];
}
template Parameter(int i)
{
// static assert(false);
}
template reflectFunction(alias Fun)
{
alias reflectFunction = Function!(
true, true, staticMap!(Parameter, 1, 2, 3));
}
void foo();
pragma(msg, reflectFunction!(foo).isPure);
Is `Parameter` instantiated in this example? If I uncomment the
assert, it fires. This is what gives me the impression that more
is instantiated than is really used. And that is probably why
reflecting runtime entities as template meta-objects with
properties for all aspects of them is slow.
I am way of my field of competence here, but I have the
impression that speeding this up would not require a change in
the language (unless it is stated that templates are greedily
expanded, and common idioms rely on this).
> you can use the -vcg-ast switch to look at how your souce code
> looks "expandend".
I tried compiling my example with `dmd -vcg-ast -c tmt.d` and I
did got neither an AST nor an error, and the option is not in
`dmd -h`, where can I read about it?
> They already gained traction, unfortunately.
Well...Two things. I ended up developing this approach because I
needed to copy function attributes, UDAs, function parameter
storage classes and UDAs to a generated function. If the
language, or Phobos, provided me a more direct way, trust me, I
would have used it. For example, storage classes are not part of
a parameter's type. OK. Now you can use `Parameter!F[0]` to
declare a parameter in a new function (great!), but you cannot
say `__traits(getStorageClasses, F)[0] Parameter!F[0]`.
Also, one of D's selling points is that it does templates and
meta-programming better. It's only natural that people use these
features then (note that in C++, sorry for mentioning it, in my
example Parameter would *not* be instantiated).
More information about the Digitalmars-d-announce
mailing list