First experience with std.algorithm: I had to resort to writing a 'contains' function.
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue Jun 8 07:32:08 PDT 2010
On 06/08/2010 08:45 AM, Steven Schveighoffer wrote:
> On Tue, 08 Jun 2010 07:23:39 -0400, Bernard Helyer <b.helyer at gmail.com>
> 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.
I agree. Someone please file this so it doesn't fall through the cracks.
Andrei
More information about the Digitalmars-d
mailing list