www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11279] New: Error: no [] operator overload for type Tuple!(int, int, int)

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11279

           Summary: Error: no [] operator overload for type Tuple!(int,
                    int, int)
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: thelastmammoth gmail.com



void main(){
  import std.typecons;
  auto a=tuple(0,1,2);
  a[0]=0;//OK
  foreach(i;0..3)
    a[i]=0;//Error: no [] operator overload for type Tuple!(int, int, int)}
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 15 2013
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11279


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |monarchdodra gmail.com
         Resolution|                            |INVALID



for typetuple, operator[] is a "static" operator: The index *must* be known at
compile time, as the return type depends on the parameter. It's actually
different overloads for each different index:

EG:

auto a = tuple('a', 5, 2.2);
char c = a[0]; //Calls the first arg
int i = a[1]; //Calls the second arg
double d = a[2]; //Calls the last arg

Because of this putting an operator[] inside a for loop is not possible, as you
are basically trying to solve overloads dynamically, which is not possible in a
static language like D.

What could be a workaround for you is the static foreach construct:

void main(){
  import std.typecons, std.typetuple;
  auto a=tuple(0,1,2);
  a[0]=0;//OK
  foreach(I;TypeTuple!(0, 1, 2))
    a[I]=0;//Error: no [] operator overload for type Tuple!(int, int, int)}
}

What this does is generate a "type sequence", which contains the "Types" (or in
this case, value parameters) 1, 2 and 3.

Now, the foreach is statically iterating over the parameters, generating a
*unique* body for each loop index. Notice that this time, I used the name "I"
this denotes that this argument is actually statically known inside the body of
the loop.

Closing as invalid.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 16 2013