www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - problem with reduce on array of Tuples

reply Adrian Matoga <epi student.agh.edu.pl> writes:
The following code fails to compile:

    double sim(Document doc, string query)
    {
        alias Tuple!(double, "wij", double, "wiq") Weights;
        Document q = Document.fromString(query);
        Weights[] wi;
        foreach (s; StrFilt(query))
            wi ~= Weights(doc.termFreq(s) * invDocFreq(s), q.termFreq
(s) * invDocFreq(s));
        return
            reduce!((double acc, Weights w) { return acc + 0; /*w.wij
* w.wiq;*/ })(0.0, wi) /
            (sqrt(reduce!((double acc, Weights w) { return acc + 0; /
*sqr(w.wij);*/ })(0.0, wi) *
             sqrt(reduce!((double acc, Weights w) { return acc + 0; /
*sqr(w.wiq);*/ }(0.0, wi)))));
    }

and DMD 2.046 shows the following messages:
ir.d(119): Error: delegate std.algorithm.__dgliteral3 cannot access
frame of fun
ction __dgliteral3
ir.d(232): Error: template instance ir.Index.sim.Reduce!(delegate
double(double
acc, Tuple!(double,"wij",double,"wiq") w)
{
return acc + 0;
}
) error instantiating
ir.d(119):        instantiated from here: reduce!(delegate double
(double acc, Tu
ple!(double,"wij",double,"wiq") w)
{
return acc + 0;
}
)


Is it my fault because of some misunderstanding or other error, or
should I report it as a bug in compiler/library?
The same function compiled well with reduce on StrFilt(query) instead
of Weights.

I attach the full source.
begin 644 ir.d
M:6UP;W)T('-T9"YS=&1I;SL-"FEM<&]R="!S=&0N<W1R:6YG.PT*:6UP;W)T
M('-T9"YC;VYV.PT*:6UP;W)T('-T9"YF:6QE.PT*:6UP;W)T('-T9"YM871H




M(F$B+"`B=&AE(BP (G=H870B+"`B8GDB+"`B;V8B+"`B86YD(BP (F]R(BP 
M(G1O(BP (F9R;VTB+"`B9&\B+"`B>65S(BP (FYO(BP (FYO="(L(")B=70B
M+"`B:70B+"`B:6XB+"`B:2(L(")Y;W4B+"`B:&4B+"`B<VAE(BP (G1H97DB


M;B`N9FEL=&5R(2 H82D >R!R971U<FX 82`A:6X <')E<',[('TI*'-P;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DH,"XP+"!W:2D +PT*"0D)*'-Q
M<G0H<F5D=6-E(2 H9&]U8FQE(&%C8RP 5V5I9VAT<R!W*2![(')E='5R;B!A
M8V, *R`P.R`O*G-Q<BAW+G=I:BD[*B\ ?2DH,"XP+"!W:2D *B!S<7)T*')E
M9'5C92$H*&1O=6)L92!A8V,L(%=E:6=H=', =RD >R!R971U<FX 86-C("L 
M,#L +RIS<7(H=RYW:7$I.RHO('TH,"XP+"!W:2DI*2DI.PT*"7T-" D-" ED

M"61O=6)L95M$;V-U;65N=%T <F5S=6QT.PT*"0EF;W)E86-H("AD;V,L(&([



M;&ES=&1I<B B9&]C<R(L(&1E;&5G871E(&)O;VP *$1I<D5N=')Y*B!D92D 
M>PT*"0EW<FET96QN*&1E+FYA;64I.R`-" D):6YD97 N861D*$1O8W5M96YT

M"0T*"69O<F5A8V  *&8[("A3=')&:6QT*&%R9W-;,5TI*2D >R!W<FET96QN



`
end
May 27 2010
parent Pelle <pelle.mansson gmail.com> writes:
This has nothing to do with the tuples, I think.

test2.d:

import std.algorithm;

void main() {
     map!((int x){return x+1;})([1,2,3,4,5]);
}

pp ~/dee% rdmd test2.d
test2.d(4): Error: delegate std.algorithm.__dgliteral1 cannot access 
frame of function __dgliteral1
test2.d(4): Error: template instance test2.main.map!(delegate int(int x)
{
return x + 1;
}
) error instantiating


It's a bug with the delegate literals inside the template.
May 27 2010