0

I have Scala Play application and I am invoking the Scala Akka Scheduler in the class like this:

@Singleton
class Foo @Inject() (
  actorSystem: ActorSystem,
)(implicit
  executionContext: ExecutionContext,
 ) extends Logging {

  actorSystem.scheduler.schedule(delay, interval)(poller())

  def poller(): Unit ...
}

But I get a compiler warning like this

discarded non-Unit value
[error]   actorSystem.scheduler.schedule(delay, interval)(poller())

Is there a way to suppress this or code this in some way to eliminate the compiler error?

Mojo
  • 1,152
  • 1
  • 8
  • 16

1 Answers1

2

This happens because the Scheduler.schedule method returns a Cancellable instance to allow the job to be cancelled, but this code ignores the return value. When the compiler flag -Ywarn-value-discard or -Wvalue-discard is set, this issues a warning. If the compiler also has -Xfatal-warnings or -Werror set, these warnings become errors.

One workaround is to explicitly ignore the return value, rather than implicitly:

val _ = actorSystem.scheduler.schedule(delay, interval)(poller())
Tim Moore
  • 8,958
  • 2
  • 23
  • 34