Make using actions typesafe
See original GitHub issueAs I tried to move over Diode, one of the errors I faced was passing action type as a parameter instead of an action object. With actions type expected to be of the AnyRef type, there was no help from the compiler. I kept trying to figure out why my handler didn’t get invoked.
Perhaps an example (not using Diode, but illustrates the problem, nevertheless) will help (which, btw, happens with Akka and similarly typed systems as well).
def handle: PartialFunction[AnyRef, Unit] = {
case Foo(i) => println(s"Received Foo with $i")
}
case class Foo(i: Int)
handle(Foo(5))
Received Foo with 5
scala> handle(Foo) // Ouch... no compiler error
scala.MatchError: Foo (of class Foo$)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:253)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:251)
at $anonfun$handle$1.applyOrElse(<console>:12)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
... 33 elided
With Diode, I don’t get runtime error either since any unhandled type is eventually silently handled.
The solution is quite simple, I think. Just introduce a marker trait Action and change all AnyRefs expecting an action to Action. Since Diode is very new I don’t think that will pose too much burden on people already using it and I doubt that anyone will want to use arbitrary types to signify actions (for example, a String type), so asking to extend Action for all action type seems like a small price to pay for type safety.
On a related note, it will make reading Diode code easier. Currently, there are too many AnyRefs out there and that type doesn’t guide in figuring such the code.
Issue Analytics
- State:
- Created 8 years ago
- Comments:22 (10 by maintainers)

Top Related StackOverflow Question
There is a good reason for all the time poured into Akka Typed I guess 😃
This is now resolved in 1.0.0 release