Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Last active February 26, 2021 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkfrog26/85f3b37ba152e3202c8f5ee80166c098 to your computer and use it in GitHub Desktop.
Save darkfrog26/85f3b37ba152e3202c8f5ee80166c098 to your computer and use it in GitHub Desktop.
package hierarchical.rw
import hierarchical.*
import hierarchical.rw.*
import scala.deriving.*
import scala.compiletime.*
trait CompileRW {
implicit val stringR: Readable[String] = stringRW
inline def ccRW[T <: Product](using Mirror.ProductOf[T]): ReadableWritable[T] = new ClassRW[T] {
override protected def t2Map(t: T): Map[String, Value] = toMap(t)
override protected def map2T(map: Map[String, Value]): T = ???
}
inline def toMap[T <: Product](t: T)(using p: Mirror.ProductOf[T]): Map[String, Value] = {
toMapElems[T, p.MirroredElemTypes, p.MirroredElemLabels](t, 0)
}
inline def toMapElems[A <: Product, T <: Tuple, L <: Tuple](a: A, index: Int): Map[String, Value] = {
inline erasedValue[T] match
case _: (hd *: tl) =>
inline erasedValue[L] match
case _: (hdLabel *: tlLabels) =>
val hdLabelValue = constValue[hdLabel].asInstanceOf[String]
val hdValue = a.productElement(index).asInstanceOf[hd]
val hdReadable = summonInline[Readable[hd]]
val value = hdReadable.read(hdValue)
toMapElems[A, tl, tlLabels](a, index + 1) ++ Map(hdLabelValue -> value)
case EmptyTuple => sys.error("Not possible")
case EmptyTuple => Map.empty
}
}
[error] -- Error: /home/mhicks/projects/open/hierarchy/core/shared/src/test/scala/spec/RWSpec.scala:45:58
[error] 45 | implicit val addressRW: ReadableWritable[Address] = ccRW[Address]
[error] | ^^^^^^^^^^^^^
[error] | cannot reduce summonFrom with
[error] | patterns : case t @ _:hierarchical.rw.Readable[String]
[error] | This location contains code that was inlined from package.scala:144
[error] | This location contains code that was inlined from CompileRW.scala:28
[error] | This location contains code that was inlined from CompileRW.scala:18
[error] | This location contains code that was inlined from CompileRW.scala:13
[error] one error found
[error] one error found
[error] (Test / compileIncremental) Compilation failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment