These are chat archives for etorreborre/specs2

23rd
Feb 2017
Daniel Spasojevic
@dspasojevic
Feb 23 2017 01:06
Is the intention that it would then get pulled in via the view bounds / implicit parameters on the >> / in methods of BlockExample? I think that would mean that I have to implement both CommandLineAsResult and AsResult?
Eric Torreborre
@etorreborre
Feb 23 2017 01:23
Yes and you just need the AsResult instance
Daniel Spasojevic
@dspasojevic
Feb 23 2017 02:06

Yep. Awesome. That works until I have using a custom context for the block (?). e.g :

class AsResultSpec(implicit val ev: ExecutionEnv) extends Specification {

  implicit def dbioAsResult[R: AsResult]: AsResult[DBIOAction[R, NoStream,Effect with Effect]] = new AsResult[DBIOAction[R,NoStream,Effect with Effect]] {
    override def asResult(r: => DBIOAction[R, NoStream,Effect with Effect]): Result = ???
  }


  "Doing the dbio" should {

    "use the implicit as result" in {
      for {
         x <- DBIO.successful(10)
         y <- DBIO.successful(5)
      } yield {
        x + y must_=== 20
      }
    }
  }

}

works.

But

class AsResultSpec(implicit val ev: ExecutionEnv) extends Specification {

  implicit def dbioAsResult[R: AsResult]: AsResult[DBIOAction[R, NoStream,Effect with Effect]] = new AsResult[DBIOAction[R,NoStream,Effect with Effect]] {
    override def asResult(r: => DBIOAction[R, NoStream,Effect with Effect]): Result = ???
  }


  "Doing the dbio" should {

    "use the implicit as result" in new Context {
      for {
         x <- DBIO.successful(10)
         y <- DBIO.successful(5)
      } yield {
        x + y must_=== 20
      }
    }
  }

}

trait Context extends Scope {

}

does not.

Eric Torreborre
@etorreborre
Feb 23 2017 06:40

That's because there's only so much you can do with implicits. In the body of an anonymous class (new Context) you need to throw exception if you want to see anything reported. There's another syntactic alternative

"use the implicit as a result" in { val c = DBContext(); import c._
  for {
    x <- DBIO.successful(10)
    y <- DBIO.successful(5)
  } yield x + y must_=== 20
}

where DBContext is a case class containing the variables you need

Daniel Spasojevic
@dspasojevic
Feb 23 2017 08:37
Thanks. I think I understand.
For Around in particular, the body of the anonymous class ends up getting run through resultOrSuccess before any searching for implicits takes place and I see the comment in mutable.Around explaining what you've said about needing to through exceptions.