I have two classes: Guid and UserGuid. Guid has one type argument. UserGuid is a special case of Guid, which represents an entity (User) for which there is no class, so I implemented it as Guid [Any].
I have several methods that are applicable to Guid that I would like to split between the two types, so I put them in a superclass (GuidFactory). However, since Guid is parameterized, I have to parameterize the GuidFactory attribute, otherwise the resulting Guid will be parameterized as Guid [_].
As a result, my companion UserGuid object does not compile, complaining that:
error: com.wixpress.framework.scala.UserGuid does not accept type parameters, expected: one UserGuid object extends GuidFactory [UserGuid]
Is there a way to share application methods between Guid and UserGuid, or should I duplicate them or use a listing?
The code follows.
abstract class TypeSafeId[I, T](val id: I) extends Serializable
class Guid[T](override val id: String) extends TypeSafeId[String, T](id)
class UserGuid(override val id: String) extends Guid[Any](id)
trait GuidFactory[I[A] <: Guid[A]] {
def apply[T](id: String): I[T]
def apply[T](id: UUID): I[T] = apply(id.toString)
def apply[T](ms: Long, ls: Long): I[T] = apply(new UUID(ms, ls))
def apply[T](bytes: Array[Byte]):I[T] = apply(UUID.nameUUIDFromBytes(bytes))
def random[T] = apply[T](UUID.randomUUID())
}
object Guid extends GuidFactory[Guid] {
override def apply[T](id: String) = new Guid[T](id)
}
object UserGuid extends GuidFactory[UserGuid] {
override def apply(id: String) = new UserGuid(id)
}
source
share