scala - Avro Serialization from generics -


this simple example based on github page of avro4s, , works fine.

object main extends app {    case class ingredient(name: string, sugar: double, fat: double)   case class pizza(name: string, ingredients: seq[ingredient], vegetarian: boolean, vegan: boolean, calories: int)    val pepperoni = pizza("pepperoni", seq(ingredient("pepperoni", 12, 4.4), ingredient("onions", 1, 0.4)), false, false, 98)   val hawaiian = pizza("hawaiian", seq(ingredient("ham", 1.5, 5.6), ingredient("pineapple", 5.2, 0.2)), false, false, 91)    val format = recordformat[pizza]   val recordinavro = format.to(pepperoni)   printf(s"avro of pepperoni: $recordinavro\n") } 

but this:

object main extends app {    case class ingredient(name: string, sugar: double, fat: double)   case class pizza(name: string, ingredients: seq[ingredient], vegetarian: boolean, vegan: boolean, calories: int)    val pepperoni = pizza("pepperoni", seq(ingredient("pepperoni", 12, 4.4), ingredient("onions", 1, 0.4)), false, false, 98)   val hawaiian = pizza("hawaiian", seq(ingredient("ham", 1.5, 5.6), ingredient("pineapple", 5.2, 0.2)), false, false, 91)    def toavro[t](obj: t): genericrecord = recordformat[t].to(obj)    val recordinavro = toavro[pizza](pepperoni)   printf(s"avro of pepperoni: $recordinavro\n") } 

however, though last piece of code seems fine, following error during compilation time:

error:(14, 54) not find implicit value parameter torecord: com.sksamuel.avro4s.torecord[t]   def toavro[t](obj: t): genericrecord = recordformat[t].to(obj) error:(14, 54) not enough arguments method apply: (implicit torecord: com.sksamuel.avro4s.torecord[t], implicit fromrecord: com.sksamuel.avro4s.fromrecord[t])com.sksamuel.avro4s.recordformat[t] in object recordformat. unspecified value parameters torecord, fromrecord.   def toavro[t](obj: t): genericrecord = recordformat[t].to(obj) 

any tips on how make avro serialisation work generics? thank you.

try change definition to

def toavro[t: torecord: fromrecord](obj: t): genericrecord = recordformat[t].to(obj) 

more info here , here

in nutshell: when using concrete type compiler automatically find required torecord implicit value. in parameterized code should promise compiler supply implicit value somehow.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -