Skip to content

Instantly share code, notes, and snippets.

@hrhino
Last active August 23, 2017 03:44
Show Gist options
  • Save hrhino/d6a658212a523b9dbfcf26aa6103532f to your computer and use it in GitHub Desktop.
Save hrhino/d6a658212a523b9dbfcf26aa6103532f to your computer and use it in GitHub Desktop.
hhoughton-de@ import $ivy.`com.chuusai::shapeless:2.3.2`; import shapeless._
import $ivy.$ ;
import shapeless._
hhoughton-de@ import shapeless._
import shapeless._
hhoughton-de@ import shapeless.ops.hlist._
import shapeless.ops.hlist._
hhoughton-de@ import shapeless.ops.record._
import shapeless.ops.record._
hhoughton-de@
hhoughton-de@ def caseClassToString[CC <: Product, Repr <: HList, Keys <: HList](cc: CC)(
implicit
generic: LabelledGeneric.Aux[CC, Repr],
keys: Keys.Aux[Repr, Keys],
trav0: ToTraversable.Aux[Repr, List, String],
trav1: ToTraversable.Aux[Keys, List, Symbol],
): String = {
val elts = (keys.apply.toList(trav1) zip generic.to(cc).toList(trav0))
.map({ case (k, v) => s"${k.name}=$v" })
.mkString(",")
s"${cc.productPrefix}($elts)"
}
defined function caseClassToString
hhoughton-de@
hhoughton-de@ case class Bingo(dingus: String, aloiter: String)
defined class Bingo
hhoughton-de@
hhoughton-de@ caseClassToString(Bingo("bip", "din"))
res7: String = "Bingo(dingus=bip,aloiter=din)"
hhoughton-de@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment