Compile-time constness is waaay to strict!

Jarrett Billingsley jarrett.billingsley at gmail.com
Fri Jul 24 17:43:50 PDT 2009


On Fri, Jul 24, 2009 at 8:27 PM, asd<a at sd.invalid> wrote:
> I've got D2 code:
>
>> template ObjcMethodSelectorCheck(string sel, A...) {
>>       const n = countMethodArguments(sel);
>
> and:
>
> pure static uint countMethodArguments(string name) {
>                if (name.length == 0) return 0;
>                if (name[0] == ':') return 1+countMethodArguments(name[1..$]);
>                return countMethodArguments(name[1..$]);
>        }
>
> Original version of this method was pure too, but used count++. Fine, maybe compiler wasn't smart enough to understand that, but now I've rewritten it in purely functional style, and it's still not "constant" enough!
>
> objc/method.d(54): Error: cannot evaluate countMethodArguments(ToSetterSelector) at compile time
> objc/method.d(57): Error: expression 1u != countMethodArguments(ToSetterSelector) is not constant or does not evaluate to a bool

I think something else is going on.  I can't reproduce any problems:

pure static uint countMethodArguments(string name)
{
    if(name.length == 0)
        return 0;

    if(name[0] == ':')
        return 1 + countMethodArguments(name[1 .. $]);

    return countMethodArguments(name[1 .. $]);
}

template Temp(string sel)
{
    const Temp = countMethodArguments(sel);
}

void main()
{
    writeln(Temp!("foo:bar:")); // prints 2
}

Can you post more of your code?



More information about the Digitalmars-d mailing list