Feb 17, 2017

Human Friendly errors for Scala

Sometimes Scala compiler errors can be too cryptic and scare beginners away

error: diverging implicit expansion for type scala.collection.generic.CanBuildFrom[ird.replay.gui.GraphType.ValueSet,(ird.replay.gui.GraphType.Value, org.jfree.data.time.TimeSeries),That]
starting with method newCanBuildFrom in object SortedSet

Or

[error] TheNextFacebook.scala:16: type mismatch;
[error]  found   : akka.http.scaladsl.server.StandardRoute
[error]  required: akka.stream.scaladsl.Flow[akka.http.scaladsl.model.HttpRequest,akka.http.scaladsl.model.HttpResponse,Any]
[error]   Http().bindAndHandle(r, "localhost", 8080)

What if we could have right now an human friendly explanation of the error, similar to what Elm or React do? (Well, even Scala itself with Dotty will have it but in the future)

Scala Clippy is a compiler (or SBT) plugin that does just that.

Just import it in your project

addSbtPlugin("com.softwaremill.clippy" % "plugin-sbt" % "0.5.1")

and the previous error magically becomes

[error]  Clippy advises: did you forget to define an implicit akka.stream.ActorMaterializer?
[error]  It allows routes to be converted into a flow.
[error]  You can read more at http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0/scala/http/routing-dsl/index.html

But there is no magic, it's all community driven.
When first started Clippy will download the current advice list from
scala-clippy.org.
It's community driven so you can also contribute!

For more detailed instructions on how to setup just visit the Clippy Github repository.

Keep Learning
Until next time emoji-sunglasses