question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make using actions typesafe

See original GitHub issue

As 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:closed
  • Created 8 years ago
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
PerWiklandercommented, Feb 29, 2016

There is a good reason for all the time poured into Akka Typed I guess 😃

0reactions
ochronscommented, Jun 20, 2016

This is now resolved in 1.0.0 release

Read more comments on GitHub >

github_iconTop Results From Across the Web

piotrwitek/typesafe-actions - GitHub
Easily create completely typesafe Actions or even Async Actions; No boilerplate and completely typesafe Reducers; Game-changing Helper Types for Redux ...
Read more >
typesafe-actions examples - CodeSandbox
Learn how to use typesafe-actions by viewing and forking example apps that make use of typesafe-actions on CodeSandbox. ; typesafe-actions-reference-project ...
Read more >
typesafe-actions - npm
Typesafe Action Creators for Redux / Flux Architectures (in TypeScript). ... Start using typesafe-actions in your project by running `npm i ...
Read more >
How to use the typesafe-actions.createAsyncAction function in ...
To help you get started, we've selected a few typesafe-actions.createAsyncAction examples, based on popular ways it is used in public projects.
Read more >
Make your redux actions type-safe | by Jakob Gaard Andersen
An action is a simple object with a type and a payload . We keep these generic, as actions will be of different...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found