digitalmars.D.learn - static foreach
- James Japherson (27/27) Oct 10 2018 I have a static opApply in a class and I need to iterate over all
- Neia Neutuladh (12/12) Oct 10 2018 On 10/10/2018 04:03 PM, James Japherson wrote:> Says that it cannot
I have a static opApply in a class and I need to iterate over all
the members statically using foreach
// Iterates over all members
static int opApply(int delegate(cPiece) dg)
{
alias T = typeof(this);
foreach (m; __traits(derivedMembers, T))
{
import std.traits : hasStaticMember;
static if (hasStaticMember!(T, m))
{
mixin("dg(X."~m~");");
}
}
return 0;
}
This is not a problem using foreach.
But when I use static foreach it fails!!!
Says that it cannot interpret X(the class that contains the
static opApply).
the opApply is static, it does everything at compile time, so why
can't the foreach be statically used?
The whole point of using opApply was so that I wouldn't have to
write all that code each time and I could just iterate over class
members when I need to.
Oct 10 2018
On 10/10/2018 04:03 PM, James Japherson wrote:> Says that it cannot
interpret X(the class that contains the static> opApply).
It's a bit hard to diagnose the problem you're getting using that
function when we don't have the code that uses it. Or the context that's
referenced with the foreach loop there. You might want to post a larger
segment of code and the entire error message.
The error I'm seeing that might be related is: opApply is a static
member of the type. So part of the function (after mixins) reads:
dg(X.opApply);
This attempts to call X.opApply with no arguments and pass the result to
dg. But X.opApply requires an argument.
You probably want to add a `is(typeof(mixin("X." ~ m)) == cPiece)` in there.
Oct 10 2018








Neia Neutuladh <neia ikeran.org>