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!
=== Other uses === Closures have many uses: * Because closures delay evaluation—i.e., they do not "do" anything until they are called—they can be used to define control structures. For example, all of [[Smalltalk]]'s standard control structures, including branches (if/then/else) and loops (while and for), are defined using objects whose methods accept closures. Users can easily define their own control structures also. * In languages which implement assignment, multiple functions can be produced that close over the same environment, enabling them to communicate privately by altering that environment. In Scheme: <syntaxhighlight lang="scheme"> (define foo #f) (define bar #f) (let ((secret-message "none")) (set! foo (lambda (msg) (set! secret-message msg))) (set! bar (lambda () secret-message))) (display (bar)) ; prints "none" (newline) (foo "meet me by the docks at midnight") (display (bar)) ; prints "meet me by the docks at midnight" </syntaxhighlight> * Closures can be used to implement [[Object-oriented programming|object]] systems.<ref>{{cite web |url=http://okmij.org/ftp/Scheme/oop-in-fp.txt |title=Re: FP, OO and relations. Does anyone trump the others? |date=29 December 1999 |access-date=2008-12-23 |archive-url=https://web.archive.org/web/20081226055307/http://okmij.org/ftp/Scheme/oop-in-fp.txt |archive-date=26 December 2008 |url-status=dead}}</ref> Note: Some speakers call any data structure that binds a [[Scope (programming)#Lexical scoping|lexical]] environment a closure, but the term usually refers specifically to functions.
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)