Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Closure (computer programming)
(section)
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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)