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

  • SEARCH
  • Home
  • 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 6206245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:25:03+00:00 2026-05-24T05:25:03+00:00

I’m trying to use Scala (2.9.0) continuations to build a seemingly blocking API, but

  • 0

I’m trying to use Scala (2.9.0) continuations to build a seemingly blocking API, but that actually is asynchronous. Suppose that you would like to write something like:

if(ask("Continue?")) //Prompts Yes/No
  name = input("Enter your name")

Where ask returns a boolean if the user pressed yes, and input asks for a value. Picture this being called from a web server, where ask and input do not block any threads, they just store a continuation in a Map (or the session, doesn’t matter much) before displaying the page with the prompt (releasing most resources). And when a response get’s back, it looks-up the continuation in the Map and resumes the code.

The problem so far is that I cannot seem to be able to find a suitable way to define ask and input to use continuations without passing the calling context’s return type as a parameter.

The closest I got is doing something like:

#!/bin/sh
exec scala -P:continuations:enable -deprecation "$0" "$@"
!#
import util.continuations._

//Api code
def display[T](prompt: String) = shift {
  cont: (Unit => T) => {
        println(prompt)
        cont()
    }
}

//Client code
def foo() : Int = reset {
  display[Int]("foo!") // <-- how do I get rid of the type annotation?
  5
}

def bar() : Unit = reset {
  display[Unit]("bar!")
}

println(foo())
bar()

I really would like to get rid of the type annotation on calls to display. Does anyone know of a way of achieving this? I don’t care if the API definition gets uglier, as long as the client code gets simpler.
Thanks!

  • 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-24T05:25:04+00:00Added an answer on May 24, 2026 at 5:25 am

    I finally figured it out:

    #!/bin/sh
    exec scala -P:continuations:enable -deprecation "$0" "$@"
    !#
    import util.continuations._
    
    class Display(val resume: (Unit => Any)) extends Throwable
    
    //Api code
    def display(prompt: String) = shift {
      cont: (Unit => Any) => {
            println(prompt)
            throw new Display(cont)
        }
    }
    
    //Client code
    def foo() : Int = reset {
      display("foo!")
      5
    }
    
    def bar() : Unit = reset {
      display("bar!")
    }
    
    //Framework
    try {
        foo()
    } catch {
        case d: Display => println(d.resume())
    }
    
    try {
        bar()
    } catch {
        case d: Display => d.resume() 
    }
    

    The trick is accepting methods that return Any (Homeresque: D’oh!) and returning Nothing.

    If you want to implement something that returns a value, such as ask, you can do:

    class Ask(val resume: (Boolean => Any)) extends Throwable
    
    //Api code
    def ask(prompt: String) = shift {
      cont: (Boolean => Any) => {
            println(prompt)
            throw new Ask(cont)
        }
    }
    

    In the above code, ask returns a Boolean.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.