Thread overview
More IndexedBy
Feb 22, 2015
Nordlöw
Feb 22, 2015
Nordlöw
Feb 22, 2015
Nordlöw
February 22, 2015
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 *Index*.

However there's something wrong implicit conversions because

    assert(jx[J(0)] == 11);

errors as

typecons_ex.d(144,14): Error: function typecons_ex.IndexedBy!(int[], Index!ulong).IndexedBy.opIndex is not callable because it is annotated with @disable
typecons_ex.d(144,14): Error: void has no value
typecons_ex.d(144,12): Error: incompatible types for ((jx.opIndex(Index(0LU).this(0LU)._ix)) == (11)): 'void' and 'int'

I'm not sure what the problem with my design is.

I'm guessing a key line is

    alias _ix this;

in Index. Should I remove this line and suffix all references to I-variables in *IndexedBy* with ._ix? If so supporting index types (*I*) other than *Index* in *IndexedBy* needs some glue.
February 22, 2015
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/gayfjaslyairnzrygbvh@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);
}
February 22, 2015
On Sunday, 22 February 2015 at 10:54:10 UTC, Nordlöw wrote:

I got things working better (without alias this in Index) at

https://github.com/nordlow/justd/blob/a3b0df924fd774709ab7844e6c47b5a8e5ff3917/typecons_ex.d

than with my previous solution (with alias this in Index) at
https://github.com/nordlow/justd/blob/a896a23901e5b922982350f423ac9400f2046ec6/typecons_ex.d