workaround for closure problem needed
BCS
ao at pathlink.com
Wed Dec 26 17:57:04 PST 2007
Reply to Mandel,
> Hi,
>
> I have a problem with closures and D 1.024.
> D2 does have full closure support, but I have to stick with D1
> for now.
> Problem: variable "state" changes silently when the anonymous
> function goes out of the scope of function filter.
> Therefore the program does print out "item" only once.
> My question is if there is a workaround ("static state" wouldn't be
> thread safe)?
>
wrap the stuff (state + the local function) in a local struct, new off an
instance at the top of the outer function, convert the outer function's access
to "state" into "inst.state" and then replace the anon delegate with "&inst.fn".
Crude and a bit ugly but it works.
OTOH (WARNING: TOTAL HACK) if you want to go one step cheaper; state is 32bits,
the same as a void*.
struct Wrap
{
bool func(A item)
{
Cast c;
c.pt = this;
return (item.getState == c.ui); //should always return true
}
}
union Cast {Wrap* pt; uint ui;}
//to get dg;
Cast c;
c.ui = state;
&c.pt.func;
this works only if state is the only thing used and if it is not changed.
More information about the Digitalmars-d-learn
mailing list