std.algorithm issue

Brian Myers bmyers at harryanddavid.com
Thu May 22 13:48:13 PDT 2008


Hmmm, for some reason this is still not working. It must be because the reduce statement is itself within a method of the same class as Merge. Here's what I've got.

class ExternalSorter : Sorter {
.
.
.

    string Merge(string f1, string f2) {
    .
    .
    .
    }

    override void Sort() {
    .
    .
    .
        auto mrg = &Merge;
        retfile = (chunk_list.length == 1 ? chunk_list[0] :
                   reduce!(mrg)(chunk_list[0], chunk_list[1..$]));
    .
    .
    .
    }

And here's the error message:
c:\dmd\bin\..\src\phobos\std\algorithm.d(283): template std.algorithm.reduce(F...) is not a function template
d_dedup.d(191): template std.algorithm.reduce(F...) cannot deduce template function from argument types !(&this.Merge)(i
nvariant(char)[],invariant(char)[][])
d_dedup.d(191): Error: incompatible types for ((this.chunk_list[cast(uint)0]) ? ((reduce(F...))((this.chunk_list[cast(ui
nt)0]),this.chunk_list[cast(uint)1..__dollar]))): 'invariant(char)[]' and 'int'

This is the closest I've gotten yet. I've tried a number of other ways with the address of operator, etc...

Brian



Dee Girl Wrote:

> Brian Myers Wrote:
> 
> > I think this is exactly what I wanted. I needed to use the address of operator along with the instance reference.
> > 
> > Wow some of the things you can do in D (like Dee Girl's post) are almost as esoteric and obscure as Felix!
> 
> In fact it is simpler. It is just bug in dmd. The following code works.
> 
> import std.algorithm;
> 
> struct A
> {
>     int Merge(int) { return 0; }
> }
> 
> void main(string[] args)
> {
>     A a;
>     auto fun = &a.Merge;
>     map!(fun)([1, 2, 3]);
> }
> 
> It is a problem that compiler can work with fun but not direct with &a.Merge. It is good idea to report bug to Walter. Such bug limits power of higher order functions. Thank you, Dee Girl
> 




More information about the Digitalmars-d mailing list