scala - Is it possible to update fields of any case class implementing a common trait -


Assume that we have a common property model.

  Property Model {def id: string df update: date}   

And we have 2 case classes to extend this case. Case class C1 (ID: string, updated: date, foo: string) Model Case Class C2 (ID: string, updated: date, bar: enter) model Enhance

Is it possible to write a utility function as below, which takes the model as a parameter and returns a copy with an update for the updated fields?

  Object model {def update [T> << Model> (Model: T): T = {model.copy (updated = new date) // This code does not compile. There are two problems with your code:  

    is not defined on the copy attribute, so there is a need to define some of the properties that you can use.
  1. Return a model instead of T for update , every model on its actual Subtype should know.

    You can do the model: [T]] {DIF ID: string DEF UPDATE: with date DRF date (D: Date): T} Case Class C1 (for example ):

      id: string, updated: date, foo: string) model [c1] {def withDate (d: date) = copy (updated = d)} case class C2 (id: String, updated: Date, bar: int) is extending model [C2] {def withDate (d: Date) = copy (updated = d)} Object model {def update [T>: Model [T]] ( Model: T): T = {model.withDate (new date)) // This code does not compile. }}   

    So now it works:

      scala> Val C1 = C1 ("Exam", New Date, "FU") C1: C1 = C1 (Test, Mon 21 Apr 10:25:10 CDT 2014, FU) Scale & gt; Model.update (c1) res0: C1 = C1 (test, Mon 21 Apr 10:25:17 cdt 2014, af)    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -