小编Jer*_*emy的帖子

使用scala continuation与netty/NIO侦听器

我正在使用Netty库(GitHub的第4版).它在Scala中运行良好,但我希望我的库能够使用延续传递样式进行异步等待.

传统上使用Netty你会做这样的事情(一个示例异步连接操作):

//client is a ClientBootstrap
val future:ChannelFuture = client.connect(remoteAddr);
future.addListener(new ChannelFutureListener {
    def operationComplete (f:ChannelFuture) = {
        //here goes the code that happens when the connection is made   
    }
})
Run Code Online (Sandbox Code Playgroud)

如果你正在实现一个库(我是),那么你基本上有三个简单的选项,允许库的用户在建立连接后做东西:

  1. 只需从connect方法返回ChannelFuture并让用户处理它 - 这不会提供netty的大量抽象.
  2. 将ChannelFutureListener作为connect方法的参数,并将其添加为ChannelFuture的侦听器.
  3. 将回调函数对象作为connect方法的参数,并从您创建的ChannelFutureListener中调用它(这将使得回调驱动的样式有点像node.js)

我想做的是第四种选择; 我没有在上面的计数中包含它,因为它并不简单.

我想使用scala分隔的continuation来使库有点像一个阻塞库,但它将在幕后无阻塞:

class MyLibraryClient {
    def connect(remoteAddr:SocketAddress) = {
        shift { retrn: (Unit => Unit) => {
                val future:ChannelFuture = client.connect(remoteAddr);
                future.addListener(new ChannelFutureListener {
                    def operationComplete(f:ChannelFuture) = {
                        retrn();
                    }   
                });
            }
        }   
    }
}
Run Code Online (Sandbox Code Playgroud)

想象一下,其他读/写操作以相同的方式实现.这样做的目的是用户的代码看起来更像这样:

reset {
    val conn = new …
Run Code Online (Sandbox Code Playgroud)

continuations scala netty

9
推荐指数
1
解决办法
2137
查看次数

Scala延续:序列中有许多变化

我一直试图用scala continuation来解决复杂的输入问题.我一直在阅读我能找到的所有材料,包括延续包中的参考文档.我想我已经在某种程度上弄明白了,当你想到这一点时它会让你觉得有些意义.

我认为我对它的理解(以及我的一些问题)可以通过这个程序得到最好的总结:

package com.whatever;
import scala.util.continuations._;

object methods {

  /* The method takes an Int as its parameter.  Theoretically, at some point in the future,
   * it will return a Float to the remainder of the continuation.  This example does it
   * immediately but doesn't have to (for example it could be calling a network service
   * to do the transformation)
   * 
   * Float @cpsParam[Unit,Float] means that whatever part of the reset{} that is captured
   * as a closure …
Run Code Online (Sandbox Code Playgroud)

continuations scala continuation-passing

6
推荐指数
1
解决办法
623
查看次数

无形'延迟和默认参数导致隐式解析失败

我的一个项目使用scala功能混合,看起来不能很好地混合在一起:

  • 类型类和无形自动类型类实例派生
  • 隐式转换(为具有类型类实例的类型添加有用的语法)
  • 默认参数,因为即使它们通常是一件坏事,它们在这里也太方便了

我遇到的问题是类型类实例派生失败,如果:

  • 未明确指定默认参数
  • 无形推导用途 Lazy

这是我可以编写的用于重现问题的最小代码量:

Show.scala

import shapeless._

trait Show[A] {
  def show(a: A): String
}

object Show {
  def from[A](f: A => String): Show[A] = new Show[A] {
    override def show(a: A) = f(a)
  }

  implicit val intShow: Show[Int] = Show.from(_.toString)

  implicit def singletonShow[A](implicit
    sa: Show[A]
  ): Show[A :: HNil] = Show.from { 
    case (a :: HNil) => sa.show(a)
  }

  implicit def singletonCaseClassShow[A, H <: HList](implicit
    gen: Generic.Aux[A, H],
    sh: Lazy[Show[H]]
  ): Show[A] = Show.from …
Run Code Online (Sandbox Code Playgroud)

scala shapeless

3
推荐指数
1
解决办法
530
查看次数