Created
August 25, 2016 05:25
-
-
Save labra/01d84a232a75fe0ff64c957a0362aa45 to your computer and use it in GitHub Desktop.
Example that generates Null Pointer Exception trying to find implicits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import cats._, data._ | |
import cats.implicits._ | |
abstract class Checker { | |
type Config | |
type Err | |
type Log | |
implicit val logMonoid: Monoid[Log] | |
type ReaderConfig[A] = Kleisli[Id, Config, A] | |
type WriterEC[A] = WriterT[ReaderConfig, Log, A] | |
type Check[A] = EitherT[WriterEC, Err, A] | |
def ok[A](x: A): Check[A] = EitherT.pure[WriterEC,Err,A](x) | |
def run[A](c: Check[A], config: Config): (Log,Either[Err,A]) = | |
c.value.run.run(config) | |
// I add this line in order to obtain the implicit Monad of WriterEC | |
val monadWriterEC = implicitly[Monad[WriterEC]] | |
} | |
object MyCheck extends Checker { | |
type Config = String | |
type Err = String | |
type Log = String | |
implicit val logMonoid: Monoid[Log] = new Monoid[Log] { | |
def combine(l1: Log, l2: Log) = l1 + "\n" + l2 | |
def empty = "" | |
} | |
} | |
object Test { | |
import MyCheck._ | |
val c1: Check[Int] = ok(3) | |
// If I don't add this line, it doesn't find implicit for Monad WriterEC | |
implicit val mWriterEC = monadWriterEC | |
val c2 : Check[Int] = c1.flatMap(v => ok(v + 1)) | |
val test = run(c2,"") // Generates Null pointer exception | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It can be solved using a
lazy val monadWriterEC = ...