[static] foreach scope, template declaration ?
Mathias LANG via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Sep 26 04:16:00 PDT 2014
On Thursday, 25 September 2014 at 23:08:53 UTC, SlomoTheBrave
wrote:
> a way around this is not to use anySatisfy nor the template,
> for example this works as expected:
>
> [...]
>
My problem is that in this example, attributes are in the same
order as the parameter. But this come from a code generator,
which takes class defined by the user, so I have to assume they
might not be in the correct order :)
> which is less abstruse. However I don't know if it has hurted
> your eyes too but the output lines order shows there is a
> problem too:
>
>> Current attr is: p1
>> Instantiated for: p1
>> Instantiated for: p2
>> Instantiated for: p3
>> Current attr is: p2
>> Current attr is: P3
>
> instead of
>
>> Current attr is: p1
>> Instantiated for: p1
>> Current attr is: p2
>> Instantiated for: p2
>> Current attr is: P3
>> Instantiated for: p3
>
> o!o
On Thursday, 25 September 2014 at 23:37:11 UTC, Ali Çehreli wrote:
>
> Surprisingly, that indicates that anySatisfy did instantiate
> CmpName with all three string values, meaning that perhaps we
> don't have shortcut behavior for 'bool' eponymous templates.
This is documented
(http://dlang.org/phobos/std_typetuple.html#.anySatisfy):
Evaluation is *not* short-circuited if a true result is
encountered; the template predicate must be instantiable with all
the given items.
> When I use myAnySatisfy instead of anySatisfy, I see that I am
> right: The last expression above does not stop instantiating
> after "p1". In other words, even though myAnySatisfy!(F, T[$/2
> .. $ ] is unnecessary (because the first part of || is already
> 'true'), it gets instantiated anyway.
>
> [...]
>
> This looks like an enhancement request.
>
> Ali
I didn't consider this aspect, and there's definitely ground for
optimization. However, let's say I replace the definition of
MyClass with:
----
class MyClass {
@(UDAStruct("p3"), UDAStruct("P2"), UDAStruct("p1")) // P2 is
a typo
void func(int p1, string p2, float p3) {}
}
----
In this case, anySatisfy is right to instantiate all 3 templates
on the first iteration. Here's the result:
----
$ dmd -unittest -run bug.d
Current attr is: p3
Instantiated for: p1
Instantiated for: p2
Instantiated for: p3
Current attr is: P2
Current attr is: p1
----
There is no needless instantiation in this case, yet we still
have the same behaviour (static assert not triggered).
More information about the Digitalmars-d-learn
mailing list