I have a class that represents sales orders:
class SalesOrder(val f01:String, val f02:Int, ..., f50:Date)
fXX fields are of different types. I ran into the problem of creating an audit trail of my orders. Given two instances of the class, I have to determine which fields have been changed. I came up with the following:
class SalesOrder(val f01:String, val f02:Int, ..., val f50:Date){ def auditDifferences(that:SalesOrder): List[String] = { def diff[A](fieldName:String, getField: SalesOrder => A) = if(getField(this) != getField(that)) Some(fieldName) else None val diffList = diff("f01", _.f01) :: diff("f02", _.f02) :: ... :: diff("f50", _.f50) :: Nil diffList.flatten } }
I was wondering what the compiler does with all _.fXX functions: they are instantiated only once (statically) and can be shared by all instances of my class, or will they be displayed every time I create an instance of my class?
My concern is that since I will be using multiple instances of SalesOrder, this can create a lot of garbage. Should I use a different approach?
source share