[Issue 4126] New: std.range.ElementType doesn't work with opApply

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Apr 24 17:34:37 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4126

           Summary: std.range.ElementType doesn't work with opApply
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-04-24 17:34:36 PDT ---
ElementType doesn't work with a class/struct that defines an opApply, this
prints 'void' (dmd 2.043):


import std.stdio: writeln;
import std.range: ElementType;

struct Foo {
    char stop;
    int opApply(int delegate(ref long) dg) {
        int result;
        for (long i = 0; i < stop; i++) {
            result = dg(i);
            if (result)
                break;
        }
        return result;
    }
}

void main() {
    writeln(typeid(ElementType!Foo)); // Output: void
}



A naive way to find the type given by the opApply:

template IterType(alias iterable) {
    alias ReturnType!({ foreach(x; iterable)
                            return x;
                        assert(0);
                      }) IterType;
}


A more refined way (this template is named BaseType1, because it goes down just
one level):

...
static if ( is(typeof(T.opApply)) )
        alias OpApplyType!(T) BaseType1;
...


Where:

template OpApplyType(T) {
    static if (ParameterTypeTuple!(ParameterTypeTuple!(T.opApply)[0]).length ==
1)
        alias ParameterTypeTuple!(ParameterTypeTuple!(T.opApply)[0])[0]
OpApplyType;
    else
        alias ParameterTypeTuple!(ParameterTypeTuple!(T.opApply)[0])
OpApplyType;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list