使用 Akka 时出现 Discarded non-Unit value 错误
Discarded non-Unit value Error When Using Akka
我有 Scala Play 应用程序,我正在 class 中调用 Scala Akka 调度程序,如下所示:
@Singleton
class Foo @Inject() (
actorSystem: ActorSystem,
)(implicit
executionContext: ExecutionContext,
) extends Logging {
actorSystem.scheduler.schedule(delay, interval)(poller())
def poller(): Unit ...
}
但是我收到这样的编译器警告
discarded non-Unit value
[error] actorSystem.scheduler.schedule(delay, interval)(poller())
有没有办法抑制这种情况或以某种方式对其进行编码以消除编译器错误?
发生这种情况是因为 Scheduler.schedule
method returns a Cancellable
实例允许取消作业,但此代码忽略了 return 值。设置编译器标志 -Ywarn-value-discard
或 -Wvalue-discard
时,会发出警告。如果编译器还设置了 -Xfatal-warnings
或 -Werror
,这些警告将变为错误。
一种解决方法是显式忽略 return 值,而不是隐式:
val _ = actorSystem.scheduler.schedule(delay, interval)(poller())
我有 Scala Play 应用程序,我正在 class 中调用 Scala Akka 调度程序,如下所示:
@Singleton
class Foo @Inject() (
actorSystem: ActorSystem,
)(implicit
executionContext: ExecutionContext,
) extends Logging {
actorSystem.scheduler.schedule(delay, interval)(poller())
def poller(): Unit ...
}
但是我收到这样的编译器警告
discarded non-Unit value
[error] actorSystem.scheduler.schedule(delay, interval)(poller())
有没有办法抑制这种情况或以某种方式对其进行编码以消除编译器错误?
发生这种情况是因为 Scheduler.schedule
method returns a Cancellable
实例允许取消作业,但此代码忽略了 return 值。设置编译器标志 -Ywarn-value-discard
或 -Wvalue-discard
时,会发出警告。如果编译器还设置了 -Xfatal-warnings
或 -Werror
,这些警告将变为错误。
一种解决方法是显式忽略 return 值,而不是隐式:
val _ = actorSystem.scheduler.schedule(delay, interval)(poller())