scala - How to use unapply extractor with class inheritance hierarchy -


i have trait material lot of values:

trait material {   var id : string = material.newid   var groupid: int = -999   var doctorid : option[string] = none   var baseid: option[string] = none   ... // lot of parameters }  object material {   def apply() = new materialitem()   def apply(id: string) = new materialitem() {     id = id   }   ... } 

and have child class materialitem looks doesn't doing anyting usefull, used mapping amazon dynamodb database:

class materialitem extends material {   @dynamodbignore   private implicit val scaleitemformat = json.format[scaleitem]   @dynamodbignore   private implicit val paramtypeformat = json.format[paramtype]   ...   @dynamodbhashkey(attributename = "hashid")   def gethashid: string = materialitem.hashid   def sethashid(value: string) = ...    @dynamodbrangekey(attributename = "materialid")   def getid() : string = id   def setid(value: string) = { id = value}   ... // lot of defs } 

and want add unapply method able convert json:

implicit val materialformat = json.format[material] 

how that? define unapply method?

but i'm interested in general case also, can exists child classes additional values , material have own unapply method (if requires material class, not trait - suggest it's class):

class materialchild1 extends material {       var child1 : string = ... }  class materialchild2 extends material {       var child2 : int = ... } 

my thoughts: understanding (correct if i'm wrong):

  1. each class should responsible it's values
  2. if class inherits values other class/trait should delegate unapply class (because child class shouldn't bother internals of parent)

thus:

object materialchild1 {     def unapply(m1: materialchild1) = {       val unapplyparent = // call unapply material ...        val tuple = m1.child1 '+' unapplyparent //make final tuple        some(tuple)     } }  // same way materialchild2  object material {     def unapply(m: material) = {       some(id, groupid, doctorid, baseid, ...)     } } 

is correct , how unapply dealing class inheritance in general? solutions (better/correct/more scala idiomatic)?


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -