[Issue 693] 'this' can't be used as an alias parameter for a mixin

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Apr 12 03:04:56 PDT 2011


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



--- Comment #3 from Simen Kjaeraas <simen.kjaras at gmail.com> 2011-04-12 03:01:10 PDT ---
Thinking about this some more, fixing this bug could lead to safer array
indexing for one, through a limited system of dependent types:

struct SafeArray( T ) {
    T[] data;

    struct Index( alias arr ) {
        size_t idx;
        // Add safety checks, overflow handling, etc.
    }

    Index!this makeIndex( size_t n ) {
        typeof( return ) result;
        result.idx = n;
        return result;
    }

    T opIndex( Index!this idx ) {
        return data[idx.idx];
    }
}

unittest {
    SafeArray!int arr;
    arr.data = [1,2,3,4,5,6,7,8,9,0];
    auto idx = arr.makeIndex( 3 );
    writeln( arr[idx] ); // Completely safe indexing of array, enforced by type
system.
}

There may be a problem in that compiler considers 'this' a local parameter to a
non-global template, which is currently illegal. Not sure how much of a problem
this might be in practice.

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