const debacle

Steven Schveighoffer schveiguy at yahoo.com
Mon Mar 24 07:58:51 PDT 2008


"Walter Bright" wrote
> Janice Caron wrote:
>> Fortunately, I think I've already solved the problem, a few posts up.
>> The solution in this case is:
>>
>>    T strchr(T : const(char)[])(T s, char c)
>>    {
>>         int n = s.find(c);
>>         return n == -1 ? null : s[n..$];
>>    }
>>
>> I don't see that there is anything fundamentally broken about that.
>
> It isn't broken because the return type T is const.
>
>> T
>> is any type which will implicitly cast to const(char)[]; you have
>> compile-time checking that s doesn't get modified within the function;
>> s /can/ be modified via it's return value /if s is not const/. This is
>> exactly what's being requested, is it not? Where's the problem with
>> it?
>
> The problem with that strchr is it doesn't address Steven's issue - if the 
> input is const, the output is, too. strchr is otherwise correct.

This is a slight misinterpretation.  If the input is const, I DO want it to 
return const.  The issue is if the input is mutable, I want it to return 
mutable BUT I also want to be able to specify that the compiler should 
complain if the function modifies the mutable input.

-Steve 





More information about the Digitalmars-d mailing list