Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Closure (computer programming)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Delegates (C#, VB.NET, D) === [[C Sharp (programming language)|C#]] anonymous methods and lambda expressions support closure: <syntaxhighlight lang="csharp"> var data = new[] {1, 2, 3, 4}; var multiplier = 2; var result = data.Select(x => x * multiplier); </syntaxhighlight> [[Visual Basic .NET]], which has many language features similar to those of C#, also supports lambda expressions with closures: <syntaxhighlight lang="vb.net"> Dim data = {1, 2, 3, 4} Dim multiplier = 2 Dim result = data.Select(Function(x) x * multiplier) </syntaxhighlight> In [[D (programming language)|D]], closures are implemented by delegates, a function pointer paired with a context pointer (e.g. a class instance, or a stack frame on the heap in the case of closures). <syntaxhighlight lang="D"> auto test1() { int a = 7; return delegate() { return a + 3; }; // anonymous delegate construction } auto test2() { int a = 20; int foo() { return a + 5; } // inner function return &foo; // other way to construct delegate } void bar() { auto dg = test1(); dg(); // =10 // ok, test1.a is in a closure and still exists dg = test2(); dg(); // =25 // ok, test2.a is in a closure and still exists } </syntaxhighlight> D version 1, has limited closure support. For example, the above code will not work correctly, because the variable a is on the stack, and after returning from test(), it is no longer valid to use it (most probably calling foo via dg(), will return a 'random' integer). This can be solved by explicitly allocating the variable 'a' on heap, or using structs or class to store all needed closed variables and construct a delegate from a method implementing the same code. Closures can be passed to other functions, as long as they are only used while the referenced values are still valid (for example calling another function with a closure as a callback parameter), and are useful for writing generic data processing code, so this limitation, in practice, is often not an issue. This limitation was fixed in D version 2 - the variable 'a' will be automatically allocated on the heap because it is used in the inner function, and a delegate of that function can escape the current scope (via assignment to dg or return). Any other local variables (or arguments) that are not referenced by delegates or that are only referenced by delegates that do not escape the current scope, remain on the stack, which is simpler and faster than heap allocation. The same is true for inner's class methods that reference a function's variables.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Closure (computer programming)
(section)
Add topic