IFTI with D2?

Steven Schveighoffer schveiguy at yahoo.com
Mon Nov 5 18:58:46 PST 2007


"Nathan Reed" wrote
> Frank Benoit wrote:
>> The const thing makes IFTI hard.
>>
>> uint searchPattern(T)( T[] arg1, T[] arg2 ){ ... }
>>
>> The function shall accept mutable/const/invariant data for both 
>> arguments.
>>
>> searchPattern( "abc".idup, "a".idup ); // (1) compiles
>> searchPattern( "abc".dup,  "a".dup  ); // (2) compiles
>> searchPattern( "abc".idup, "a".dup  ); // (3) doesn't compile
>>
>> (1) and (2) will result in independant version of the function?
>>
>> Is there a solution/workaround?
>> Is this a compiler bug?
>
> That's correct, isn't it?  You have the function declared as taking the 
> same type T[] for both parameters, but in (3) the arguments are of 
> different types.
>
> Does it work if you do:
>
> unit searchPattern (T,U) (T[] arg1, U[] arg2) { ... }
>
> Thanks,
> Nathan Reed

Yes, but then this also compiles when it shouldn't:

int[] x = {1};
searchPattern("abc", x);

In addition, The real requirement is that Frank wants to templatize the 
array element type, but not its constness.  For example, if the template 
pattern was:

uint searchPattern(T)(const(T)[] arg1, const(T) arg2){...}

This still wouldn't compile for line 3, even though it would be perfectly 
legal to cast both arguments to const.  The challenge is to express 
something that says the function will not alter the arguments, but you also 
want to template the argument type.

-Steve 





More information about the Digitalmars-d mailing list