www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6274] New: 'pure' for a whole struct definition

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6274

           Summary: 'pure' for a whole struct definition
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



Just like "final" applied to a class makes all its methods non-virtual:


final class Foo {
    int x;
    this(int xx) { x = xx; }
    void bar() {}
    static void spam() {}
}
void main() {}



I'd like the attribute "pure" applied to a struct/class (in DMD 2.054beta this
doesn't compile):

pure struct Foo {
    int x;
    this(int xx) { x = xx; }
    void bar() {}
    static void spam() {}
}
void main() pure {
    Foo f = Foo(1);
    f.bar();
    Foo.spam();
}


to be equivalent to adding "pure" to all its methods:

struct Foo {
    int x;
    this(int xx) pure { x = xx; }
    void bar() pure {}
    static void spam() pure {}
}
void main() pure {
    Foo f = Foo(1);
    f.bar();
    Foo.spam();
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 08 2011
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6274




There is something I don't fully understand. The following code compiles with
the improvements in DMD 2.055alpha/head, but it needs a "pure" or before

removed it doesn't compile. So is the pure attribute for struct partially
working already?



 property bool empty(T)(in T[] a) pure nothrow {
    return !a.length;
}

 property ref T front(T)(T[] a) pure nothrow {
    assert(a.length);
    return a[0];
}

void popFront(A)(ref A a) pure nothrow {
    assert(a.length);
    a = a[1 .. $];
}


    R _input;

    this(R input) nothrow pure {
        _input = input;
    }

     property bool empty() nothrow const pure {
        return _input.empty;
    }


        return fun(_input.front);
    }

    void popFront() nothrow pure {
        _input.popFront();
    }
}

template map(alias fun) {
    auto map(R)(R range) {
        return Map!(fun, R)(range);
    }
}

int sqr(int x) pure nothrow { return x * x; }

pure nothrow int foo(int n) {
    int total;
    foreach (x; map!(sqr)([1, 2, 3, 4]))
        total += x;
    return total;
}

void main() {
    assert(foo(10) == 30);
}

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