On Thursday, 14 December 2017 at 16:38:26 UTC, Steven Schveighoffer wrote:
So enumerate returns as its element type a Tuple. Specifically, it's going to be a Tuple!(size_t, int), since you are enumerating an array of ints.

I'm not sure why you are having the error, compiling your code above works perfectly fine for me. It would help to know:

a) which version of the compiler you are using?

Tried with dmd v2.077.1. Also with LDC 1.6 (which uses dmd v2.076.1)

b) if the above actually does compile for you, what is the minimal code that does not?

Yes of course, here's a minimal program that does not compile for me:

  import std.stdio, std.algorithm, std.range, std.array;

  int rotate(int[] lengths) {
    foreach(skip, length; lengths.enumerate) {}
    return 0;
  }

  auto knotHash(string input) {
    return [1].rotate();
  }

  auto data = ["string"]
    .map!knotHash
    .array;

  void main() {}


The above code, however, will compile with any of the following changes, and I don't understand why for any of them:
1) remove .enumerate
2) move the auto data inside the body of main
3) remove the call to .array

All that aside, you may not realize, this works as well:

foreach(skip, length; lengths)

Sweet, thanks! Yeah that works too.

Another thing I realized is that if I switch from .enumerate to the foreach you suggest (in my non minimized example) the compile time increases by A LOT. From about 1 second to 70 seconds.


Reply via email to