On Sunday, 22 February 2015 at 10:52:26 UTC, Nordlöw wrote:
At

https://github.com/nordlow/justd/blob/master/typecons_ex.d#L93

I'm trying to implement a wrapper type that provides type-safe indexing and slicing via an integer-like wrapper type I call

Here's a copy of the definitions for reference:

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

import std.traits: isIntegral, isInstanceOf;
import std.range: hasSlicing;

enum IndexableBy(R, I) = (hasSlicing!R &&
(isIntegral!I || // TODO should we allo isIntegral here?
                           isInstanceOf!(Index, I) ||
                           is(I == enum)));

/** Wrapper for $(D R) with Type-Safe $(D I)-Indexing.
See also: http://forum.dlang.org/thread/gayfjaslyairnzryg...@forum.dlang.org#post-gayfjaslyairnzrygbvh:40forum.dlang.org
    TODO Support indexing by tuples
    TODO Use std.range.indexed
   */
struct IndexedBy(R, I) if (IndexableBy!(R, I))
{
    alias RI = size_t; /* TODO: Extract this from R somehow. */

    auto ref opIndex(I ix) inout { return _r[ix]; }
auto ref opIndexAssign(V)(V value, I ix) { return _r[ix] = value; }

auto ref opSlice(I lower, I upper) inout { return _r[lower .. upper]; } auto ref opSliceAssign(V)(V value, I lower, I upper) { return _r[lower .. upper] = value; }

    static if (!is(RI == I))
    {
        @disable void opIndex(RI i);
        @disable void opIndexAssign(V)(V value, RI i);
        @disable void opSlice(RI i, RI j);
        @disable void opSliceAssign(V)(V value, RI i, RI j);
    }

    R _r;
alias _r this; // TODO Use opDispatch instead; to override only opSlice and opIndex
}

/** Instantiator for $(D IndexedBy).
   */
auto indexedBy(I, R)(R range) if (IndexableBy!(R, I))
{
    return IndexedBy!(R, I)(range);
}

Reply via email to