Skip to content

Instantly share code, notes, and snippets.

@ruslanbogun
Created December 24, 2018 08:25
Show Gist options
  • Save ruslanbogun/7402a70ea9696b9e88d3dadcc65bd048 to your computer and use it in GitHub Desktop.
Save ruslanbogun/7402a70ea9696b9e88d3dadcc65bd048 to your computer and use it in GitHub Desktop.
import scodec.bits.BitVector
import scodec.{Attempt, Codec}
import scodec.codecs.int32L
trait T
case class One(a: Int) extends T
object One {
implicit val codecOne: Codec[One] =
int32L.as[One]
}
case class Two(a: Int, b: Int) extends T
object Two {
implicit val codecTwo: Codec[Two] =
(int32L :: int32L).as[Two]
}
trait Common[A <: T] {
def encode(implicit a: Codec[A]): Attempt[BitVector]
}
trait Adaptation[A <: T] extends Common[A] {
def fun: A
def encode(implicit a: Codec[A]): Attempt[BitVector] = {
Codec.encode(fun)
}
}
class AdaptationTestOne extends Adaptation[One] {
def fun: One = One(1)
}
class AdaptationTestTwo extends Adaptation[Two] {
def fun: Two = Two(2, 3)
}
object AdaptationTest {
def main(args: Array[String]): Unit = {
val testOne = new AdaptationTestOne
val testTwo = new AdaptationTestTwo
println(testOne.encode)
println(testTwo.encode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment