Why doesn't this work in D2?

Jacob Carlborg doob at me.com
Sun Jun 27 13:56:54 PDT 2010


On 2010-06-27 19:26, Simen kjaeraas wrote:
> Jacob Carlborg <doob at me.com> wrote:
>
>> Why doesn't the following code work in D2 (it works in D1)?
>>
>> void foo (T) (in T[] a, T b)
>> {
>>
>> }
>>
>> void main ()
>> {
>> "asd".foo('s');
>> }
>>
>> The error I get is:
>>
>> main.d(10): Error: template main.foo(T) does not match any function
>> template declaration
>> main.d(10): Error: template main.foo(T) cannot deduce template
>> function from argument types !()(string,char)
>>
>> It seems to be some problem with the "b" argument, if I change that to
>> "char" it works.
>
> In D2, strings are of type immutable(char)[], so your T would be
> immutable(char). However, 's' is a simple, unadorned char.
>
> Ways to fix this would include:
>
> void foo(T)(const T[] a, const T b){
> ...
> }
>
> void foo(T,U)(const T[] a, U b) if (is(Unqual!T == Unqual!U)) {
> ...
> }

That's annoying, specially since "char" is a value type. I would 
preferably have a solution for both D1 and D2. Can I use a template to 
cast/alias away the immutable part?


-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list