Talk:Closure (computer programming): Difference between revisions

Content deleted Content added
Cewbot (talk | contribs)
m Maintain {{WPBS}}: 2 WikiProject templates. Keep majority rating "C" in {{WPBS}}. Remove 2 same ratings as {{WPBS}} in {{WikiProject Computer science}}, {{WikiProject Computing}}.
 
(9 intermediate revisions by 9 users not shown)
Line 1:
{{Skiptotoctalk}}
{{WikiProject banner shell|class=C|1=
{{WikiProjectBannerShell|1=
{{WikiProject Computer science|class=C |importance=Mid}}
{{WikiProject Computing |classimportance=CLow |software=y |software-importance=Mid}}
}}
{{To do}}
Line 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 117 ⟶ 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.
<sourcesyntaxhighlight lang="java">
class CalculationWindow extends JFrame {
private volatile int result;
Line 133 ⟶ 135:
}
}
</syntaxhighlight>
</source>
[[Special:Contributions/76.235.207.239|76.235.207.239]] ([[User talk:76.235.207.239|talk]]) 09:11, 29 May 2011 (UTC)
 
Line 604 ⟶ 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. <font color="darkgreen">[[User:Lulu of the Lotus-Eaters|LotLE]]</font>×<fontspan colorstyle="darkred" size="-2color:darkgreen;">LotLE</span>]]×[[User talk:Lulu of the Lotus-Eaters|<span style="color:darkred; font-size:x-small;">talk]]</fontspan>]] 18:17, 21 July 2007 (UTC)
 
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 610 ⟶ 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. <font color="darkgreen">[[User:Lulu of the Lotus-Eaters|LotLE]]</font>×<fontspan colorstyle="darkred" size="-2color:darkgreen;">LotLE</span>]]×[[User talk:Lulu of the Lotus-Eaters|<span style="color:darkred; font-size:x-small;">talk]]</fontspan>]] 15:18, 9 August 2007 (UTC)
 
== Closures and Objects ==
Line 673 ⟶ 675:
 
: To see a closure does not just "remember values" try:
<sourcesyntaxhighlight lang="python">
Python 3.1.2 (release31-maint, Sep 17 2010, 20:27:33)
[GCC 4.4.5] on linux2
Line 703 ⟶ 705:
>>> d()
2
</syntaxhighlight>
</source>
: —''[[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 804 ⟶ 806:
 
: 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)