Skip to content

Instantly share code, notes, and snippets.

@vmarquez
Last active December 14, 2015 15:18
Show Gist options
  • Save vmarquez/5106252 to your computer and use it in GitHub Desktop.
Save vmarquez/5106252 to your computer and use it in GitHub Desktop.
Broken Scala For Comprehension
package vsxmpp
import scalaz.EitherT
import scalaz._
import Scalaz._
case class ABC(s:String)
object ABC {
implicit val m = new Monoid[(ABC, Int)] {
def zero: (ABC, Int) = (null, -1)
def append(f1: (ABC, Int), f2: => (ABC, Int)): (ABC, Int) = f1
}
def BrokenMethod(): EitherT[Option, (ABC, Int), (ABC, String)] = {
EitherT(Some((ABC("abcData"),"Success").right))
}
def filterComp() =
BrokenMethod()
.filter {
case (abc,"Success") => true
case _ => false
}.map {
case (abc, "Success") => "yay"
}
def forComp() =
for {
(a,b) <- BrokenMethod() //erroneous or inaccessible type
} yield "yay"
def main(args: Array[String]): Unit = {
val a = forComp()
println("a = " + a)
}
}
@retronym
Copy link

Here's a closer look at things:

case class ABC(s:String)

object Test {
  def BrokenMethod(): XEitherT[Option, (ABC, Int), (ABC, String)] = ???

  trait XMonoid[F]
  trait XFunctor[F[_]]
  implicit def OptionFunctor: XFunctor[Option] = ???
  implicit val xm: XMonoid[(ABC, Int)] = ???

  trait XEitherT[F[_], A, B] {
    def filter[AA >: A](p: B => Boolean)(implicit M: XMonoid[AA], F: XFunctor[F])
  }

  BrokenMethod().withFilter(_ => true) // okay

  locally {
    import scalaz._
    import Scalaz._

    BrokenMethod().filter(_ => true) // okay
  }

  locally {
    import scalaz._
    import Scalaz._

    BrokenMethod().withFilter(_ => true) // erroneous or inaccessible type.
  }
}

Something imported from Scalaz._ seems to be interfering with the rewriting of withFilter to filter. Scalaz should really define a withFilter method directly; this rewriting is a backward compatibility measure in scalac, as the rewriting of for-comprehensions changed in 2.8. Take a look at the withFilter implementation for, say, scala.Option to see what they should look like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment