【问题标题】:Scala IntelliJ warning "dynamic invocation could be replaced with a constructor invocation"Scala IntelliJ 警告“动态调用可以替换为构造函数调用”
【发布时间】:2018-01-05 21:35:34
【问题描述】:

为什么 Intellij 会给出这个警告,这是什么意思,我怎样才能让它变得更好?

import akka.actor.Props

object Router {
  def props(config: Config, addresses: Set[Address]): Props =
    Props(classOf[Router], config, addresses)
    // "dynamic invocation could be replaced with a constructor invocation"

如果我使用 Props,我会收到不同的警告。

system.actorOf(Props(classOf[Router], config, addresses))
// could be replaced with factory method call

谢谢

【问题讨论】:

    标签: scala intellij-idea akka


    【解决方案1】:

    您是否缺少带有 Config 实例和 Address 实例集合的构造函数的 class Router 定义?比如……

    class Router(config: Config, addresses: Set[Address]) extends Actor
    

    如果是这样,请尝试对您的伴随对象进行以下轻微修改。

    object Router {
    
      def props(config: Config, addresses: Set[Address]): Props = {
        Props(new Router(config, addresses))
      }
    
    }
    

    这遵循best practices 创建演员,可能会摆脱警告。

    【讨论】:

      猜你喜欢
      • 2022-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多