Universal supertype of companion facility in Scala

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)
}
+5
source share
2 answers

This is the best I can offer:

import java.util.UUID
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[G] {
  def apply(id: String): G
  def apply(id: UUID): G = apply(id.toString)
  def apply(ms: Long, ls: Long): G = apply(new UUID(ms, ls))
  def apply(bytes: Array[Byte]): G = apply(UUID.nameUUIDFromBytes(bytes))
  def random = apply(UUID.randomUUID())
}

object Guid {
  def apply[T] = new GuidFactory[Guid[T]] {
    def apply(id: String) = new Guid[T](id)
  }
}

object UserGuid extends GuidFactory[UserGuid] {
  override def apply(id: String) = new UserGuid(id)
}

val guid1 = Guid[String]("123")
+2
source

Did this solve your problem ?:

package guid
import java.util.UUID

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[Nothing](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)
//}

object Test {
  val guid1 = Guid[String]("123")
  val guid2 = Guid.random[List[Any]]
  val userguid = Guid("123")
  val userguid2 = Guid.random
}
+1
source

All Articles