如何定义更简洁的scala函数

发布于 2024-12-10 22:41:46 字数 709 浏览 1 评论 0原文

我正在使用 akka 库并提供一个部分函数,​​以便由参与者在运行时通过热交换来实现。

akka 热交换采用 PartialFunction[Any, Unit] 形式的参数。我的定义如下:

class Fake1Reader extends AbstractReader {

  def read: PartialFunction[Any, Unit] = {
    case readingRequest: ReadingRequest => {
      var reading: Reading = new ReadingImpl(readingRequest.getResourceId, "payload",
        Calendar.getInstance.getTime,
      readingRequest.getfrequency, readingRequest.getMappingName,
        readingRequest.getClassificationType,
      readingRequest.getReadingRequestId)
      sendConsumeMessage(reading)
    }
  }
}

所以为了使用这个函数,我必须提供一个新的 Fake1Reader().read。

有没有更简洁的方法使用 apply 或扩展 Function 或 PartialFunction 来执行此类?

I am using the akka library and supplying a partial function to be implemented by an actor at runtime via a hot swap.

The akka hot swap takes an argument in the form PartialFunction[Any, Unit]. I have defined mine as the following:

class Fake1Reader extends AbstractReader {

  def read: PartialFunction[Any, Unit] = {
    case readingRequest: ReadingRequest => {
      var reading: Reading = new ReadingImpl(readingRequest.getResourceId, "payload",
        Calendar.getInstance.getTime,
      readingRequest.getfrequency, readingRequest.getMappingName,
        readingRequest.getClassificationType,
      readingRequest.getReadingRequestId)
      sendConsumeMessage(reading)
    }
  }
}

so in order to use this function I have to supply a new Fake1Reader().read.

Is there any more concise way of doing this class using apply or extending Function or PartialFunction?

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

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

发布评论

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

评论(2

夏末 2024-12-17 22:41:46

如果您的 AbstractReader 是无状态的,您可以定义 object 而不是 class 以避免每次使用时创建不必要的对象,并将函数放置在那里作为不可变的val 的。

此外,伴随对象akka.actor.Actor将类型Receive定义为PartialFunction[Any, Unit]的别名,因此您可以编写您的部分功能如下:

package foo    
object Fake1Reader extends AbstractReader {
  import akka.actor.Actor._

  val read: Receive = { 
    case readingRequest: ReadingRequest => /* implementation */
  }
}

用法:

import foo.Fake1Reader._

actorRef ! HotSwap(read)

If your AbstractReader is stateless, you can define object instead of class to avoid unnecessary object creation on every usage,and place your functions there as immutable val's.

Also, companion object akka.actor.Actor defines type Receive as an alias for PartialFunction[Any, Unit], so you can write your partial functions like this:

package foo    
object Fake1Reader extends AbstractReader {
  import akka.actor.Actor._

  val read: Receive = { 
    case readingRequest: ReadingRequest => /* implementation */
  }
}

usage:

import foo.Fake1Reader._

actorRef ! HotSwap(read)
憧憬巴黎街头的黎明 2024-12-17 22:41:46

顺便说一句,这减少了重复的苦差事:

case readingRequest: ReadingRequest => {
  import readingRequest._
  var reading: Reading = new ReadingImpl(getResourceId, "payload",
    Calendar.getInstance.getTime,
  getfrequency, getMappingName,
    getClassificationType,
  getReadingRequestId)
  sendConsumeMessage(reading)
}

As a side note, this reduces the sheer drudgery of repetition:

case readingRequest: ReadingRequest => {
  import readingRequest._
  var reading: Reading = new ReadingImpl(getResourceId, "payload",
    Calendar.getInstance.getTime,
  getfrequency, getMappingName,
    getClassificationType,
  getReadingRequestId)
  sendConsumeMessage(reading)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文