digitalmars.D - IFTI with D2?
- Frank Benoit (9/9) Nov 05 2007 The const thing makes IFTI hard.
- Nathan Reed (8/22) Nov 05 2007 That's correct, isn't it? You have the function declared as taking the
- Steven Schveighoffer (13/36) Nov 05 2007 Yes, but then this also compiles when it shouldn't:
- Frank Benoit (7/21) Nov 07 2007 Yes exactly.
- Steven Schveighoffer (31/40) Nov 07 2007 I have some more thoughts on this.
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?
Nov 05 2007
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
Nov 05 2007
"Nathan Reed" wroteFrank Benoit wrote: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. -SteveThe 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
Nov 05 2007
Steven Schveighoffer schrieb: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. -SteveYes exactly. And additionally it should be possible to create functions with specific arguments be const or mutable with same type. T[] replace(T)( const(T)[] str, const(T)[] pattern, const(T)[] repl, T[] output = null ){ .... } In this case the last arg must be mutable.
Nov 07 2007
"Frank Benoit" wroteThe 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?I have some more thoughts on this. I think part of the problem is that casting X to const X might be interpreted the same as casting X to some other type Y which is implicitly cast-able (i.e. casting char[] to const(char)[] is the same as casting int to long). Since any type can be cast to a const version of that type, and the cast does NOT alter the data type in terms of storage, I think there should be some special rules regarding const for IFTI. For instance, if you have an argument to a template function that is a const argument, like so: f(T)(const(T)[] arg1) Then calling f with a mutable T, a const T or an invariant T should result in the SAME template instantiation. I think this is doable, but only for const. Basically, this is done by the compiler recognizing that the template argument is going to be const, so the compiler should cast the argument to const before attempting to instantiate the template. In code this reads: f(x); is equivalent to: f(cast(const)x); This won't fix your example, but in essence, your second line wouldn't compile unless it's arguments were const anyways, so the example really should be: uint searchPattern(T)( const(T)[] arg1, const(T)[] arg2 ){ ... } Regarding your concern about specifying that an argument must be mutable, well, that should work itself out. When the compiler attempts to instantiate a template version with a non-mutable type, it will fail to compile at the point where you change it. So I don't think you need a special interpretation by the compiler. So I think this one fix will solve the problem. I will add this enhancement to the D bug system, and we'll see what happens. -Steve
Nov 07 2007