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.
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