www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24323] New: x[0 .. 2] cast to Ty[2], x[$-2 .. $] does not?

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

          Issue ID: 24323
           Summary: x[0 .. 2] cast to Ty[2], x[$-2 .. $] does not?
           Product: D
           Version: D2
          Hardware: All
               URL: http://dlang.org/
                OS: All
            Status: NEW
          Severity: normal
          Priority: P3
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: turkeyman gmail.com

void fn(ref const ubyte[2]);

void test(ubyte[] data)
{
  fn(data[0 .. 2]); // <- call successfully
  fn(data[$-2 .. $]); // <- fails: not callable using argument types `ubyte[]`

  // this also fails:
  size_t i = data.length - 2;
  fn(data[i .. i+2]);

  // this works:
  fn(data[$-2 .. $][0 .. 2]);
}

I feel like the language should be able to determine that `$ - ($-2) == 2`. The
workaround is pretty dumb.
I can't possibly be the first one to report this, what's the reason? Just hard
to implement the bit of algebra required?
As long as there is just one variable term in a slice expression, and the rest
of the terms are all known at compile time, this equation can be easily solved.

I sure hope that $ symbol is only ever evaluated once inside a slice
expression, even in the presence of a custom opDollar?

--
Jan 07