Talk:D (programming language)
|
|
![]() | Computing Start‑class | |||||||||
|
![]() | Computer science Start‑class Low‑importance | ||||||||||||||||
|
HelloWorld
Where is HelloWorld? —Preceding unsigned comment added by 12.15.136.26 (talk) 21:03, 14 October 2010 (UTC)
- D's Hello World is pretty boring. I think it'd only waste space in the article. --Vladimir (talk) 01:05, 23 October 2010 (UTC)
Purity of mySum function in the "Functional" section (1.1.4)
I am not a D programmer, so I may misunderstand the language's semantics, but I am confused about how the mySum function could be considered pure:
int main()
{
int[] a1 = [0,1,2,3,4,5,6,7,8,9];
int[] a2 = [6,7,8,9];
int pivot = 5;
pure int mysum(int a, int b) // pure function
{
if (b <= pivot) // ref to enclosing-scope
return a + b;
else
return a;
}
The value mySum produces depends on the value of pivot from the enclosing scope. If the numerical value of pivot in mySum was fixed at the time mySum is defined, then mySum would be only depend on its arguments, and could arguably be called a pure function, but from what I infer from the section on nested functions on the function page of D's reference manual, the value of pivot in mySum is the value of pivot in the enclosing scope at the time mySum is called.
So I would expect that:
pivot = 4;
mySum(3, 5);
would yield 3, but
pivot = 5;
mySum(3, 5);
would yield 8
Unfortunately I don't have a D compiler installed on my computer, so I cannot check this myself. —Preceding unsigned comment added by 67.168.77.169 (talk) 08:16, 6 November 2010 (UTC)
- The code indeed compiles. I think that the idea is that nested functions have a hidden argument - a pointer to their enclosing scope (main's local variables). However, that doesn't explain why the code continues to compile when pivot is moved outside main(), or if you add a call to a non-pure function in mySum - these sound like compiler bugs. --Vladimir (talk) 11:45, 6 November 2010 (UTC)