I am experimenting with Akka and have the following code snippet:
private val behavior = Behaviors.supervise(SupervisedActor()).onFailure(SupervisorStrategy.restart)
And I get a:
No ClassTag available for Nothing
on the onFailure
method.
I can solve this as follows (verbose typing to make things clear):
private val strategy: Behaviors.Supervise[String] = Behaviors.supervise(SupervisedActor())
private val behavior: Behavior[String] = strategy.onFailure[Throwable](SupervisorStrategy.restart)
private val child = context.spawn(behavior, name = "supervised-actor")
Now if we look at the source code we see:
private final val ThrowableClassTag = ClassTag(classOf[Throwable])
final class Supervise[T] private[akka] (val wrapped: Behavior[T]) extends AnyVal {
/** Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws. */
def onFailure[Thr <: Throwable: ClassTag](strategy: SupervisorStrategy): Behavior[T] = {
val tag = classTag[Thr]
val effectiveTag = if (tag == ClassTag.Nothing) ThrowableClassTag else tag
Supervisor(Behavior.validateAsInitial(wrapped), strategy)(effectiveTag)
}
}
So it seems like if we don’t make the type explicit, the library designers set the exception for us.
So my questions are:
- why does Dotty force us to provide these tags?
- seeing as it is an implicit, is their any way to set this via an implicit val?
TIA