These are chat archives for typelevel/scala

19th
Sep 2017
Vinayak Pathak
@vinayakpathak
Sep 19 2017 16:15

what's the best way of doing this:
i want to define a "timed" version for each case class that simply adds a field called dateTime to it... do i need to pull out shapeless for this? (it does seem so because we will at the very least need to make sure that the case class doesn't already contain dateTime)

Here's a naive solution that doesn't quite work:

case class Timed[A](a: A, dateTime: DateTime)

implicit def taToA(ta: Timed[A]): A = ta.a

case class A(value: Int)

val ta = Timed(A(1), DateTime.now)

assert(ta.value == 1)
Rob Norris
@tpolecat
Sep 19 2017 16:21
I would do what you have in line 1.
I would not do the implicit conversion.
Also Timed is a traversable functor which may come in handy.
Vinayak Pathak
@vinayakpathak
Sep 19 2017 16:28
the problem with not doing the implicit conversion is that I already have code written that uses A's and B's and all sorts of case classes and I want to replace all of them with Timed[A]s and Timed[B]s without having to rewrite all the a.value and b.value to a.a.value and b.a.value
defining a functor instance for Timed will definitely make the code cleaner, but I'll still have maps everywhere. Not sure if that's clean enough!
Edmund Noble
@edmundnoble
Sep 19 2017 22:05
It's also not clean to add the datetime field to the class itself
Firstly you can't abstract over it, secondly you'll need to convert back and forth