For instance I have two tagged types:
trait Created
type CreatedDttm = LocalDateTime @@ Created
type CreatedTs = Timestamp @@ Created
This types are used to deal with models. The first one is for common model, and the second one is for db entity.
final case class Entity(created: CreatedDttm) // Common model
final case class EntityRow(created: CreatedTs) // DB model
I have a converter somewhere in my sources:
def toModel(e: EntityRow) = Entity(e.created.toLocalDateTime) // Does not compile
This convertion does not compile, cause e.created.toLocalDateTime returns LocalDateTime, but Entity needs LocalDateTime tagged by Created.
So I have to change my conversion to tag[Created](e.created.toLocalDateTime) to make this code compile. It works, but, imho, it looks a kind of ugly.
Timestamp was tagged by Created, and a new LocalDateTime must be also tagged by the same Created.
Is there any way to modify tagged type without need to retag a new modified value?
Afraid not. The best I think you can do it minimize your pain. I would use an implicit class with a method like
toCreatedDttm. Something like this:Then you can change your line to:
Or perhaps you can have the implicit class operate directly on the
Timestamplike this:Then the line could be: