Content deleted Content added
→First Example (Python): new section |
m Maintain {{WPBS}}: 2 WikiProject templates. Keep majority rating "C" in {{WPBS}}. Remove 2 same ratings as {{WPBS}} in {{WikiProject Computer science}}, {{WikiProject Computing}}. Tag: |
||
(31 intermediate revisions by 22 users not shown) | |||
Line 1:
{{Skiptotoctalk}}
{{WikiProject Computer science|class=C|importance=Mid}}▼
{{WikiProject
{{todo}}▼
{{WikiProject Computing |importance=Low |software=y |software-importance=Mid}}
}}
==Don't start with Lisp==
Line 104 ⟶ 107:
----
Im going to add a perl example back to the page unless somebody protests. [[User:Demerphq|Demerphq]] 17:05, 10 April 2006 (UTC)
Seriously? no mention at all of Perl in the article?
[[Special:Contributions/159.83.196.1|159.83.196.1]] ([[User talk:159.83.196.1|talk]]) 18:30, 14 May 2018 (UTC)
==Closures and Java==
Line 114 ⟶ 119:
Actually that java example on the anonymous class is overcomplicated for its result. The following code performs the same function without the need of creating an embedded runnable class. This is because the embedded Runnable classes run method is just called from the Thread classes run. Anyone who does threading in java knows this, which is why we can just extend thread instead of implementing Runnable. In such a case as this, it would be better formed code to just extend the thread into an anonymous, instead of creating an embedded runnable.
<
class CalculationWindow extends JFrame {
private volatile int result;
Line 130 ⟶ 135:
}
}
</syntaxhighlight>
[[Special:Contributions/76.235.207.239|76.235.207.239]] ([[User talk:76.235.207.239|talk]]) 09:11, 29 May 2011 (UTC)
Scala compiles to Java and supports Clousures. Not sure if this is an imperative language or not, but if so please append Scala to the list of "modern garbage-collected imperative languages support closures" <small><span class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:Jcalfee|Jcalfee]] ([[User talk:Jcalfee|talk]] • [[Special:Contributions/Jcalfee|contribs]]) 16:47, 22 August 2013 (UTC)</span></small><!-- Template:Unsigned --> <!--Autosigned by SineBot-->
==Closures and Python==
Python does not have closures and shouldn't even be listed among the list of languages that do have closures, let alone used as the example language to demonstrate what they are. Using Python to demonstrate what closures are is completely misleading and damaging to a reader's understanding of what closures are. Can we just swap the example language and remove Python from the list? <small><span class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:Mmachenry|Mmachenry]] ([[User talk:Mmachenry|talk]] • [[Special:Contributions/Mmachenry|contribs]]) 06:14, 25 June 2014 (UTC)</span></small><!-- Template:Unsigned --> <!--Autosigned by SineBot-->
Why Python doesn't have closures:
Line 596 ⟶ 606:
There are way too many external references at bottom, listing most everyone's blog entry on "How to do closures in language X (and what are they)". While a user of language X might want to know this, we should not try to anticipate every new programmer of every language. They can use Google, or one of the sites intended as indexes of PL resources, for that.
Absent strong objections, I'll remove about 80% of the external links pretty soon.
I agree. Only the links relevant to closures as such should remain, IMO. To be honest, I'd rather get rid of all the "how to do it in language XXX" sections in the article itself as well. [[User:Int19h|-- int19h]] 04:52, 23 July 2007 (UTC)
Line 602 ⟶ 612:
Firmly agreed, to both. "[[WP:NOT#HOWTO|How to]]" sections have strong tendency to accrete more and more languages, as visitors notice their favored ones are missing. (Look at the state the {{querylink|Currying|qs=oldid=142061535}} article was in recently.) —[[User:Piet Delport|Piet Delport]] 01:39, 24 July 2007 (UTC)
:I'm definitely in favor of also killing the "how to do it in language X" sections. Does someone else want to do that? I'd feel like I was overstepping slightly since all I've done in this particular article is delete the linkfarm.
== Closures and Objects ==
Line 665 ⟶ 675:
: To see a closure does not just "remember values" try:
<
Python 3.1.2 (release31-maint, Sep 17 2010, 20:27:33)
[GCC 4.4.5] on linux2
Line 695 ⟶ 705:
>>> d()
2
</syntaxhighlight>
: —''[[User:Ruud Koot|Ruud]]'' 13:11, 2 April 2011 (UTC)
: Also, Finkel gives a pretty similar definition to the current one: "A procedure in combination with its nonlocal referencing environment is called a '''closure'''." And then continues with "Because this idea is unfamiliar to students who mainly use C (which has no nested procedures, and therefore no nonlocal referencing environments), I will present several examples." I think we should use the same approach. —''[[User:Ruud Koot|Ruud]]'' 19:31, 2 April 2011 (UTC)
Line 747 ⟶ 757:
:: I do think that the final part of the first sentence ("for the non-local variables of that function.") needs to be clarified. In particular it might be possible to give a definition of "referencing environment" instead. Something along the lines of "a map containing [[name binding|binding]]s for the [[non-local variable]]s of that function."?—''[[User:Ruud Koot|Ruud]]'' 21:47, 28 December 2011 (UTC)
::I have taken the liberty to remove the terms ''function value'' and ''functional value'' from the first sentence. These terms do not usually refer to closures, except of course in the context of languages in which all function values are closures, but there are many languages in which this is not the case. [[User:Rp|Rp]] ([[User talk:Rp|talk]]) 12:38, 31 March 2012 (UTC)
== First Example (Python) ==
Line 753 ⟶ 765:
But I am worried that the following snippet that is used as the first example in the article may have an error. Can someone please help me understand where the value for 'y' is initialized in the incrementer() function? I understand that the lambda function would have access to 'x', which is defined within it's parent function. But, in the first call to (initialization of) incrementer, the value of y=undefined, but the snippet seems to indicate that it is initialized to 0(int).
def incrementer(x=1):
return lambda y: x+y
incr1 = incrementer(1)
print incr1(2) # 3
incr2 = incrementer(2)
print incr2(3) # 5
[[Special:Contributions/166.248.77.56|166.248.77.56]] ([[User talk:166.248.77.56|talk]]) 03:26, 11 February 2012 (UTC)
:<code>lambda arguments: expression</code> is an unnamed function that behaves as if defined like this:
<pre>def name(arguments):
return expression</pre>
:So <code>incrementer(1)</code> is itself a function which adds 1 to its argument (named <code>y</code>). [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 06:12, 11 February 2012 (UTC)
: The example was recently replaced. My [http://en.wikipedia.org/w/index.php?title=Closure_%28computer_science%29&oldid=473219244#Example old example] was IMHO clearer. The other editor apparently found it [http://en.wikipedia.org/w/index.php?title=Closure_%28computer_science%29&action=historysubmit&diff=475069300&oldid=473219244 too verbose], but this was intentional as it has to be understandable to non-Python programmers too. —''[[User:Ruud Koot|Ruud]]'' 11:14, 11 February 2012 (UTC)
: Also, the new example fails to demonstrate that the state captured in a closure can be mutable. I'll restore the old example. —''[[User:Ruud Koot|Ruud]]'' 11:19, 11 February 2012 (UTC)
::I agree that the lambda code is too mysterious to be helpful here, and the example you just restored is better. [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 01:59, 12 February 2012 (UTC)
: I find the [https://en.wikipedia.org/w/index.php?title=Closure_(computer_programming)&oldid=595411369#Example | current example] misleading since the closure returned by <code>counter</code> is an impure function, printing different values for calls on the same argument. The fact that the state captured in a closure can be mutable is important, and should be mentioned somewhere in the article, but presenting this as a canonical example is likely to mislead readers into thinking that closures are by nature impure (misleading since purely functional languages like Haskell of course allow closures as well). —[[User:Smerdis|Smerdis]] 27 February 2014 <span style="font-size: smaller;" class="autosigned"> — Preceding [[Wikipedia:Signatures|undated]] comment added 20:26, 27 February 2014 (UTC)</span><!--Template:Undated--> <!--Autosigned by SineBot-->
: This article has several significant problems.
For one thing it frequently bases its assertions about whether some language x, has closures with how they are implemented in said language. For example, when one says that D and C# use delegates in this context, one suggests that delegates are somehow fundamentally related to the semantics of closures. Delegates are a technique to implement function references in object a fashion which implicitly captures the object to which the method belongs, allowing it to be invoked in a type safe and polymorphic manner on the correct instance/static. C# captures represents references to functions via delegates, but that is not why it has closures. It has closures because its function values are lexically scoped such that, when invoked, they execute in the lexical context in which they were defined. The fact that they can be, and usually are, stored in delegate instances is merely a low level detail.
Additionally the question of purity vs impurity of functions is a broader topic.
[[User:Rahab rx|Aluan Haddad]] ([[User talk:Rahab rx|talk]]) 08:47, 20 March 2014 (UTC)Rahab_Rx
== First example needs one more information ==
I think that the first example with ''function startAt(x)'' should explain from where the variable ''y'' comes and how it gets its value changed. This is implicitly told below the example by giving the result for <code>closure<sub>1</sub></code> & <code>closure<sub>2</sub></code>, but a verbal explanation is missing.--[[User:Sae1962|Sae1962]] ([[User talk:Sae1962|talk]]) 12:12, 5 February 2015 (UTC)
== Functions in C -- Clarity recommended... ==
''Closures typically appear in languages in which functions are first-class values—in other words, such languages enable functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers.''
Earlier, it is indicated that 'c' is not such a language, and does not allow nested functions, but, in essence, via pointers, it does. You cannot expressly pass a function, but you can pass a pointer to a function, and this allows you to do some stuff that looks remarkably indistinguishable from those used in the examples of legitimate usage of closures.
I'm not arguing agains the claim, but suggesting that it would be good to discuss this somewhere as to how it differs.[[Special:Contributions/167.230.96.8|167.230.96.8]] ([[User talk:167.230.96.8|talk]]) 20:04, 13 May 2015 (UTC)
: There is some discussion about this at [[First-class function#Non-local variables and closures]]. —''[[User:Ruud Koot|Ruud]]'' 13:28, 14 May 2015 (UTC)
I'm confused about the claim that C doesn't support nested functions; recursion is a standard technique in C, so it clearly does support nested functions. It doesn't directly support passing a function as a value, so they aren't first-class values. It doesn't support closures, due to not being able to execute the function after the enclosing function has exited, but that is the other case. This wording seems almost like a typo, but I'm not fluent enough in Algol to make that edit. Perhaps the source of my confusion is that C doesn't allow nested function definitions. If so, perhaps clarifying that would help other readers with a similar confusion.--[[User:Wcoole|Wcoole]] ([[User talk:Wcoole|talk]]) 23:08, 21 October 2015 (UTC)
:: ''Nested functions'', in this contaxt, means ''lexically'' nested functions. Last time I used C, gcc supported them while standard C didn't. (The next question is then whether such functions can be closures, i.e. use variables defined in their lexical environment when invoked outside of that scope.) [[User:Rp|Rp]] ([[User talk:Rp|talk]]) 12:27, 20 March 2018 (UTC)
== Capture-by-value in the lead section ==
{{ping|Maggyero}} I'm not sure if I completely agree with [https://en.wikipedia.org/w/index.php?title=Closure_%28computer_programming%29&type=revision&diff=729749204&oldid=723316111 these] changes to the lead section. These changes allow for the possibility of a closure capturing variables by value instead of by reference. If feel this distinction is overly pedantic for the lead section, and makes it even harder to understand. Especially considering that the distinction only makes sense at all in imperative languages/language where variables are mutable, and even more so since C++ is the only language I'm aware of that allows capturing by value. Instead I'd suggest assuming immutable variables/capture-by-reference semantics in the lead and go into more detail on capture-by-value in a footnote and/or separate section on closures in imperative languages. —''[[User:Ruud Koot|Ruud]]'' 15:08, 14 July 2016 (UTC)
== Small typos or not? ==
Hi, these two seemed to me as typos but I'm not sure, and this is my first attempt to contribute to wp.. In '''Delegates (C#, D)''' section, the sentence "For example, the above code will not work correctly, because the variable a is on the stack, and after returning from test(),..." it is not clear to which function test() refers, test1(), test2() or both? And in the last sentence "The same is true for inner's class methods that..." should it be "inner" instead of "inner's class methods" or am I missing something? --[[User:S.POROY|S.POROY]] ([[User talk:S.POROY|talk]]) 19:45, 15 May 2017 (UTC)
== Example and text don't match ==
I was reading this article and found this mismatch in section "Lexical environment". There is no <code>foo</code> function nor <code>f</code> or <code>g</code> variables. I think the text is referring to the example in section "Anonymous functions". Example and text follows:
<syntaxhighlight lang="ecmascript">
'use strict'
function makeFunc() {
const name = "Closure";
function displayName() {
console.log(name);
}
return displayName;
};
const runFunc = makeFunc();
runFunc(); //Closure
</syntaxhighlight>
Note how function<code>foo</code> and the closures referred to by variables <code>f</code> and <code>g</code> all use the same relative memory ___location signified by local variable <code>x</code>.
[[User:Bjacquet|Bjacquet]] ([[User talk:Bjacquet|talk]]) 11:59, 19 March 2018 (UTC)
|