Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workaround for a possible scala bug in show for value class. #1804

Merged
merged 5 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions core/src/main/scala/cats/Show.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cats

import simulacrum.typeclass
import cats.functor.Contravariant

/**
Expand All @@ -10,16 +9,32 @@ import cats.functor.Contravariant
* made a toString method, a Show instance will only exist if someone
* explicitly provided one.
*/
@typeclass trait Show[T] extends Show.ContravariantShow[T] {
// duplicated so simulacrum knows it requires an instance of this trait
def show(t: T): String
}
trait Show[T] extends Show.ContravariantShow[T]

/**
* Hand rolling the type class boilerplate due to https://issues.scala-lang.org/browse/SI-6260 and scala/bug#10458
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SI-6260 is scala/bug#6260

*/
object Show {
trait ContravariantShow[-T] {

def apply[A](implicit instance: Show[A]): Show[A] = instance

trait ContravariantShow[-T] extends Serializable {
def show(t: T): String
}

trait Ops[A] {
def typeClassInstance: Show[A]
def self: A
def show: String = typeClassInstance.show(self)
}

trait ToShowOps {
implicit def toShow[A](target: A)(implicit tc: Show[A]): Ops[A] = new Ops[A] {
val self = target
val typeClassInstance = tc
}
}

/** creates an instance of [[Show]] using the provided function */
def show[A](f: A => String): Show[A] = new Show[A] {
def show(a: A): String = f(a)
Expand Down
17 changes: 16 additions & 1 deletion tests/src/test/scala/cats/tests/RegressionTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package tests
import cats.data.{Const, NonEmptyList}

import scala.collection.mutable

import RegressionTests._
class RegressionTests extends CatsSuite {

// toy state class
Expand Down Expand Up @@ -123,4 +123,19 @@ class RegressionTests extends CatsSuite {
NonEmptyList.of(6,7,8).traverse_(validate) should === (Either.left("6 is greater than 5"))
checkAndResetCount(1)
}

test("#1802 duplicated method name") {
Show[First[String]]
}
}


object RegressionTests {
final case class First[A](getFirst: A) extends AnyVal
object First {
implicit def showInstance[A](implicit ev: Show[A]): Show[First[A]] = new Show[First[A]] {
def show(f: First[A]): String = s"First(${ev.show(f.getFirst)})"
}
}

}