On 01/18/2012 04:57 PM, Timon Gehr wrote:
On 01/18/2012 02:32 PM, Jerome BENOIT wrote:
On 18/01/12 04:36, Jonathan M Davis wrote:
On Wednesday, January 18, 2012 02:33:25 Jerome BENOIT wrote:
And I cannot figure why :-(
http://d.puremagic.com/issues/show_bug.cgi?id=1528
As a workaround, templatize the last function by changing its
signature to
int[] find()(int[] longer, int[] shorter)
actually it does not work either: gdmd gives an other error message now.
------------------------------------------------------------------------
T[] find(T, E)(T[] haystack, E needle)
if (is(typeof(haystack[0] != needle) == bool)) {
while (haystack.length > 0 && haystack[0] != needle) {
haystack = haystack[1 .. $];
}
return haystack;
}
TL[] find(TL, TS)(TL[] longer, TS[] shorter)
if (is(typeof(longer[0 .. 1] == shorter) : bool)) {
while (longer.length >= shorter.length) {
if (longer[0 .. shorter.length] == shorter) break;
longer=longer[1 .. $];
}
return longer;
}
int[] find()(int[] longer, int[] shorter) {
while (longer.length >= shorter.length) {
if (longer[0 .. shorter.length] == shorter) break;
longer=longer[1 .. $];
}
return longer;
}
unittest {
// Test the introduced overloads
long[] a1 = [ 6, 1, 2, 3 ];
long[] a2 = [ 1 , 2 ];
int[] b1 = [ 6, 1, 2, 3 ];
int[] b2 = [ 1 , 2 ];
assert(find(a1, a2) == a1[1 .. $]);
assert(find(a1, b2) == a1[1 .. $]);
assert(find(b1, b2) == b1[1 .. $]);
}
void main() {}
------------------------------------------------------------------------
The message is now:
searching_05.d:34: Error: template searching_05.find(T,E) if
(is(typeof(haystack[0] != needle) == bool)) find(T,E) if
(is(typeof(haystack[0] != needle) == bool)) matches more than one
template declaration, searching_05.d(9):find(TL,TS) if
(is(typeof(longer[0..1] == shorter) : bool)) and
searching_05.d(18):find()
Is partial ordering really supported ?
Yes it is, and your code snippet indeed compiles on my machine. Are you
maybe using an outdated version of the compiler?
Nevermind, I forgot to pass the -unittest switch. It indeed gives that
error. The reason it still does not compile is that the workaround
proposed by Jonathan has slightly different semantics than it would have
if the compiler already supported overloading of functions against
function templates.
If exactly one of two equally good matched functions is a templated one,
the other one is chosen. Now that you have templated the second
function, both are an equally good match and both are templated.
If you change the last signature to
int[] find(TL:int[], TS:int[])(TL longer, TS shorter)
It will compile. However, if TL or TS are user-defined types with an
alias this of type int[], the semantics are still different. I think
this bug needs special attention.