I'm trying to figure out how to implement a light-weight wrappr realizing type-safe indexing á lá Ada. Here's my first try:

struct Ix(T = size_t)
{
    @safe pure: @nogc nothrow:
    this(T ix) { this._ix = ix; }
    alias _ix this;
    private T _ix = 0;
}

struct IndexedBy(R, I)
{
    auto ref opIndex(I ix) inout { return _r[ix]; }
auto ref opSlice(I lower, I upper) inout { return _r[lower .. upper]; }
    R _r;
    alias _r this;
}

auto indexedBy(I, R)(R range)
{
    return IndexedBy!(R, I)(range);
}

unittest
{
    import std.stdio;
    auto x = [1, 2, 3];
    alias I = int;
    auto ix = x.indexedBy!I;
    ix[0] = 11;

    alias J = Ix!size_t;
    auto jx = x.indexedBy!J;
    jx[J(0)] = 11;              // should compile
jx[0] = 11; // TODO how can I make this not compile?
}

My question now of course is:

How can I prevent

    jx[0] = 11;

from compiling?

Reply via email to