( ). RightFolder
, , LabelledTypeClass
, ( , - ).
Update
, , TypeClass
, , case , . ( TypeClass
), :
scala> (123 :: "abc" :: HNil).shows
res2: String = (123, abc)
case , . , genericShow
.
. , Show
. - :
scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._
scala> val x: Int = 123
x: Int = 123
scala> s"${ x.shows }${ x.shows }"
res0: String = 123123
x
, .shows
, Show
. Scalaz Show[Int]
, , .
:
def toStringTwice[X](x: X): String = s"${ x.shows }${ x.shows }"
:
<console>:18: error: value shows is not a member of type parameter X
def toStringTwice[X](x: X): String = s"${ x.shows }${ x.shows }"
^
, , x
Show
, x
. :
scala> def toStringTwice(x: String): String = s"${ x.shows }${ x.shows }"
toStringTwice: (x: String)String
scala> def toStringTwice(x: Int): String = s"${ x.shows }${ x.shows }"
toStringTwice: (x: Int)String
...
, . , Show
, , , :
scala> def toStringTwice[X: Show](x: X): String = s"${ x.shows }${ x.shows }"
toStringTwice: [X](x: X)(implicit evidence$1: scalaz.Show[X])String
Int
, Show
:
scala> toStringTwice(123)
res2: String = 123123
, , :
def toStringFourTimes[X](x: X): String = s"${ toStringTwice(x) * 2 }"
:
scala> def toStringFourTimes[X: Show](x: X): String = s"${ toStringTwice(x) * 2 }"
toStringFourTimes: [X](x: X)(implicit evidence$1: scalaz.Show[X])String
- Show
, . toStringTwice
: Show
, Show
.
, Shapeless - .
, LabelledTypeClass
, , TypeClass
. , , .
, prettyPrint
- Show
A
( ), toString
.
, , , :
import scalaz.Show, scalaz.Scalaz._
import shapeless._
import shapeless.ops.coproduct.Folder
import shapeless.ops.hlist.RightReducer
object prettyPrint2 extends Poly2 {
implicit def defaultCase[A: Show]: Case.Aux[A, String, String] =
at[A, String]((a, z) => s"$a, $z")
}
object prettyPrint extends Poly1 {
implicit def defaultCase[A: Show]: Case.Aux[A, String] = at[A](_.shows)
}
implicit def hlistShow[L <: HList](implicit
reducer: RightReducer.Aux[L, prettyPrint2.type, String]
): Show[L] = Show.shows(l => "(" + l.reduceRight(prettyPrint2) + ")")
implicit def coproductShow[C <: Coproduct](implicit
folder: Folder.Aux[prettyPrint.type, C, String]
): Show[C] = Show.shows(_.fold(prettyPrint))
implicit def genericShow[A, R](implicit
gen: Generic.Aux[A, R],
reprShow: Show[R]
): Show[A] = reprShow.contramap(gen.to)
:
scala> Foo(123, "abc").shows
res0: String = (123, abc)
scala> (Foo(123, "abc"): Base).shows
res1: String = (123, abc)
, case .., - (. nofollow noreferrer → ). Scala ), , .