D - delegate and function pointer overloading
- Daniel Yokomiso (51/51) Nov 20 2002 Hi,
Hi,
The DMD compiler (0.50) doesn't complain when I overload a function to
accept both function pointers or delegates, as in:
bit all(char[] array, bit (*predicate)(char)) {
for (int i = 0; i < array.length; i++) {
if (!predicate(array[i])) {
return false;
}
}
return true;
}
bit all(char[] array, bit delegate(char) predicate) {
for (int i = 0; i < array.length; i++) {
if (!predicate(array[i])) {
return false;
}
}
return true;
}
bit isVowel(char c) {
return (c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c ==
'u');
}
class Character {
private char letter;
this(char c) {
this.letter = c;
}
public bit isLetter(char c) {
return this.letter == c;
}
}
int main() {
Character a = new Character('a');
bit delegate(char) isLetter;
isLetter = &a.isLetter;
// printf("%d\r\n", all("aeiouoeieuiei", &isVowel));
printf("%d\r\n", all("aeiouoeieuiei", isLetter));
return 0;
}
But if we uncomment the call to the overloaded all function using a
function pointer, the compiler says that both functions match for the
function pointer parameter. If I have just one version the code doesn't
compile, because it says it can't cast from fp to delegate and vice-versa.
Is it a bug or I'm missing something?
Best regards,
Daniel Yokomiso.
"this line has five words
and also five syllables
that line had seven"
- Liz Cordingley
Nov 20 2002








"Daniel Yokomiso" <daniel_yokomiso yahoo.com.br>