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
Continuation-passing style
(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!
===Continuations as objects=== {{See also|Callback (computer programming)}} Programming with continuations can also be useful when a caller does not want to wait until the callee completes. For example, in [[user interface]] (UI) programming, a routine can set up dialog box fields and pass these, along with a continuation function, to the UI framework. This call returns right away, allowing the application code to continue while the user interacts with the dialog box. Once the user presses the "OK" button, the framework calls the continuation function with the updated fields. Although this style of coding uses continuations, it is not full CPS.{{Clarify|date=June 2019}} <syntaxhighlight lang=javascript> function confirmName() { fields.name = name; framework.Show_dialog_box(fields, confirmNameContinuation); } function confirmNameContinuation(fields) { name = fields.name; } </syntaxhighlight> A similar idea can be used when the function must run in a different thread or on a different processor. The framework can execute the called function in a worker thread, then call the continuation function in the original thread with the worker's results. This is in [[Java (programming language)|Java]] 8 using the [[Swing (Java)|Swing]] UI framework: <syntaxhighlight lang=java> void buttonHandler() { // This is executing in the Swing UI thread. // We can access UI widgets here to get query parameters. int parameter = getField(); new Thread(() -> { // This code runs in a separate thread. // We can do things like access a database or a // blocking resource like the network to get data. int result = lookup(parameter); javax.swing.SwingUtilities.invokeLater(() -> { // This code runs in the UI thread and can use // the fetched data to fill in UI widgets. setField(result); }); }).start(); } </syntaxhighlight>
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)