使用 Scala 延续实现非阻塞 API

发布于 2024-11-26 13:54:03 字数 1062 浏览 1 评论 0原文

我正在尝试使用 Scala (2.9.0) 延续来构建看似阻塞的 API,但实际上是异步的。假设您想编写如下内容:

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

如果用户按“是”,则 ask 返回一个布尔值,并且 input 要求输入一个值。想象一下这是从 Web 服务器调用的,其中 askinput 不会阻止任何线程,它们只是在 Map 中存储延续(或会话,并不重要) much),然后显示带有提示的页面(释放大部分资源)。当响应返回时,它会在 Map 中查找延续并恢复代码。

到目前为止的问题是,我似乎无法找到一种合适的方法来定义 askinput 来使用延续,而不将调用上下文的返回类型作为参数传递。

我得到的最接近的是做类似的事情:

#!/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()

我真的很想摆脱调用 display 时的类型注释。有谁知道实现这一目标的方法?我不在乎 API 定义是否变得更丑,只要客户端代码变得更简单即可。 谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2024-12-03 13:54:03

我终于想通了:

#!/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() 
}

诀窍是接受返回 Any 的方法(Homeresque:D'oh!)并返回 Nothing

如果你想实现一些返回值的东西,比如ask,你可以这样做:

class Ask(val resume: (Boolean => Any)) extends Throwable

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

在上面的代码中,ask 返回一个Boolean

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文