Last active
August 29, 2015 14:05
-
-
Save jminer/19eaf18aaaa6e5fa7fd3 to your computer and use it in GitHub Desktop.
Referring to function pointer in definition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----- | |
Dart | |
----- | |
main() { | |
var factorial = (n) { | |
if(n == 0) | |
return 1; | |
return n * factorial(n - 1); | |
}; | |
} | |
// error: line 2 pos 6: initializer of 'factorial' may not refer to itself | |
----- | |
C# | |
----- | |
class Program { | |
static void Main() { | |
Func<int, int> factorial = delegate(int n) { | |
if(n == 0) | |
return 1; | |
return n * factorial(n - 1); | |
}; | |
} | |
} | |
// (6,24): error CS0165: Use of unassigned local variable 'factorial' | |
----- | |
D | |
----- | |
void main() { | |
auto factorial = (int n) { | |
if(n == 0) | |
return 1; | |
return n * factorial(n - 1); | |
}; | |
} | |
// (5): Error: undefined identifier factorial | |
----- | |
Dart | |
----- | |
main() { | |
var factorial; | |
factorial = (n) { | |
if(n == 0) | |
return 1; | |
return n * factorial(n - 1); | |
}; | |
} | |
// Works | |
----- | |
D | |
----- | |
void main() { | |
int delegate(int) factorial; | |
factorial = (int n) { | |
if(n == 0) | |
return 1; | |
return n * factorial(n - 1); | |
}; | |
} | |
// Works |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment