On Tue, 08 Jun 2010 07:23:39 -0400, Bernard Helyer <[email protected]> wrote:

So I'm writing a compiler, and I wanted to create a list of TokenTypes (an enum) and check to see if the current token is among them. 'A-ha!', says I, 'I'll use std.algorithm!' (as I hadn't tried it really, yet, except for playing around) So I look it up, and find 'find'. It returns an iterated range, or an empty range on failure. A little funky, but that's okay, I can swing it!

immutable TokenType[] someList = [TokenType.Foo, TokenType.Bar];

...

while (find(someList, tokenStream.peek.type) != []) {
    doStuff();
}

If I have unittests on, this assert is triggered:

static assert(is(typeof(s) == Tuple!(string, float)));

I didn't at first, so I got another error. So I looked closer at the documentation, and it turns out it needs an input range, and that needs popFront! Well, it can't popFront an immutable range, so I dropped immutable, and decided to let convention and TLS sort the rest out!

This should be filed as a bug. find should be able to strip the immutable part from the array length itself since immutable T[] implicitly casts to immutable(T)[]. I think this should be special cased.

For custom ranges, it's not as easy, but array should be supported as well as is possible.


This worked. Until I turned unittests on, and the assert I showed above tripped. At this point my language turned rather unpleasant and I wrote this:

bool contains(T)(const(T)[] l, T a)
{
      foreach(e; l) {
          if (a == e) {
             return true;
          }
      }
      return false;
}

And my problems went away. I assume what I experienced is a bug, but I'm not sure, so I thought I'd share my experience.

As D2 nears release, and is used more and more, I'm sure we will find many problems. It's sort of a chicken-and-egg thing, people don't want to use D2 because it's changing so much, but when nobody uses it, nobody finds any bugs until it's finalized! I suspect at least one more change to const is necessary to take away the pain :)

-Steve

Reply via email to