开发者

Generic companion object supertype in Scala

开发者 https://www.devze.com 2023-03-20 17:36 出处:网络
I have two classes, Guid and UserGuid. Guid has one type argument. UserGuid is a special case of Guid that represent an entity (User) for which there is no class, so I\'ve implemented it as a Guid[Any

I have two classes, Guid and UserGuid. Guid has one type argument. UserGuid is a special case of Guid that represent an entity (User) for which there is no class, so I've implemented it as a Guid[Any].

I have several apply methods for Guid which I would like to share between the two types, so I put them in a superclass (GuidFactory). However, since Guid is parameterized I have to parameterize the GuidFactory trait, otherwise the r开发者_Go百科esulting Guid will be parameterized as Guid[_].

As a result, my companion object UserGuid does not compile, complaining that:

error: com.wixpress.framework.scala.UserGuid takes no type parameters, expected: one object UserGuid extends GuidFactory[UserGuid]

Is there any way I can share the apply methods between Guid and UserGuid or must I duplicate them or use casting?

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)
}


This is the best I can suggest:

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")


Would 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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号