[Issue 16302] New: Add opStaticIndex that takes compile-time indices

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jul 20 01:43:32 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16302

          Issue ID: 16302
           Summary: Add opStaticIndex that takes compile-time indices
           Product: D
           Version: D2
          Hardware: All
               URL: http://dlang.org/
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: eyal at weka.io

Wanted to implement a RefTuple which keeps pointers to elements inside, and
allows tuple[index] to return a different type for each index, while converting
an internally-stored pointer to a result ref.

However, opIndex currently takes only runtime indices.

I suggest having:

x[i]

where 'i' is a compile-time value, compile to:

x.opStaticIndex!i() and if this fails to resolve to a method, fall back to
x.opIndex(i) (similar to the way foreach method resolution uses fallbacks from
opApply to range interface).

Of course if 'i' is a run-time value, it compiles to x.opIndex(i) as before.

The compiler could disallow defining both an opStaticIndex and an opIndex
simultaneously to guarantee saner behavior:

int x = 1;
enum y = 1;
assert(foo[x] == foo[y]);

This makes tuples less magical, and allows enriching the library with
interesting tuple-like types.

--


More information about the Digitalmars-d-bugs mailing list