Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:22:49+00:00 2026-05-22T15:22:49+00:00

Suppose I have to write some GUI code as follows: widget1.addListener(event1 => handle1(event1) widget2.addListener(event2

  • 0

Suppose I have to write some GUI code as follows:

widget1.addListener(event1 =>
   handle1(event1)
   widget2.addListener(event2 =>
     handle2(event2)
     widget3.addListener(event3 => handle3(event3))
   )
)

How would you write it in CPS-style using Scala continuations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:22:50+00:00Added an answer on May 22, 2026 at 3:22 pm

    Just wanted to provide working example in addition to other answers. With Scala continuations it can look like this:

    import scala.util.continuations._
    
    object ContinuationsExample extends App {
      val widget1 = Widget()
      val widget2 = Widget()
    
      reset {
        val event1 = widget1.getEvent
        println("Handling first event: " + event1)
        val event2 = widget2.getEvent
        println("Handling second event: " + event2)
      }
    
      widget2 fireEvent Event("should not be handled")
      widget1 fireEvent Event("event for first widget")
      widget2 fireEvent Event("event for second widget")
      widget1 fireEvent Event("one more event")
    }
    
    case class Event(text: String)
    
    case class Widget() {
      type Listener = Event => Unit
      var listeners : List[Listener] = Nil
    
      def getEvent = shift { (l: Listener) =>
        listeners = l +: listeners
      }
    
      def fireEvent(event: Event) = listeners.foreach(_(event))
    }
    

    This code actually compiles and runs, so you can try it yourself. You should receive following output:

    Handling first event: Event(event for first widget)
    Handling second event: Event(event for second widget)
    Handling first event: Event(one more event) 
    

    If you will compile this example, then don’t forget to enable continuations by providing -P:continuations:enable argument for the Scala compiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some PHP code running inside a web server, for example, running
Suppose we have some resource(a file on disk) in which we have to write
Suppose I have some Python code like the following: input = open(input.txt) x =
I am using Visual Studio 2010 and suppose I have to write some program.
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
An Example Suppose we have a text to write and could be converted to
I have the following problem: Suppose I have some basic counter class Counter .
Suppose I have some application A with a database. Now I want to add
Suppose I have a class with some attributes. How is it best (in the
Suppose i have class USER with attributes. In some examples i see UserService class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.