简体   繁体   中英

Scala - composing generic types

I want to create a generic interface which hides the implementation of the hash function on the key of a product (which is used to lookup products and for various caching and calculations outside the scope of the product itself). I want to tie the concrete implementation of the hash function to the particular reader which implements the interface. I am doing this for various consistency reasons and since the products are stored in a cache and might be migrated in the future and I want to assure that the lookup is always using the same hash . I have some syntax issues on how to compose generics in this fashion .

For example this type of hierarchy I am having syntax issues:

trait Hash {
  type U
  type V
  def hash(u:U) : V
}

trait Product[hasher <: Hash] { 
   val key : hasher#U
   // hasher.hash(x : U) => V
   def hashKey : hasher#V
}

trait ProductReader[product <: Product] { // error: type 'Product' takes parameters - but I don't want to define them at this point yet...
   def getByKey(key : U) : Product
   def getByHashed(hash : V) : Product
}

Example usage is:

object MyHash extends Hash[String,Long] {
  def hash(key : String) : Long = mySpecialHash(key)
}

// implements serialization (together with the hasher)
class MyProduct[MyHash[String,Long]](val key : String {
  def hashKey : Long = MyHash.hash(key)
}

class MyProductReader[MyProducts[MyHash[String,Long]] {
   def getByKey(key : String) : MyProduct[MyHash[String,Long]] = ....
   def getByHashed(hash : Long) : MyProduct[MyHash[String,Long]] = ...
}

You can use abstract type members instead of generic type parameters if you don't want to bind a specific Product at that point of declaration:

trait Hash {
  type U
  type V
  def hash(u: U): V
}

trait Product {
  type Hasher <: Hash

  val key: Hasher#U
  def hashKey: Hasher#V
}

trait ProductReader[P <: Product] {
  def getByKey(key: P#Hasher#U): P
  def getByHashed(hash: P#Hasher#V): P
}

object MyHash extends Hash {
  override type U = String
  override type V = Long

  def hash(key: String): Long = ???
}

class MyProduct(val hasher: MyHash.type) extends Product {
  override type Hasher = hasher.type

  def hashKey: Long = MyHash.hash(key)
  override val key: String = "MyProduct"
}

class MyProductReader extends ProductReader[MyProduct] {
  override def getByKey(key: String): MyProduct = ???
  override def getByHashed(hash: Long): MyProduct = ???
}

An additional approach would be adding an additional type parameter. It really depends on your flavor, and I think using a type member in this case ends up being less verbose.

You can add a second parameter type to ProductReader

trait Hash {
  type U
  type V
  def hash(u:U) : V
}

trait Product[hasher <: Hash] { 
   val key : hasher#U
   // hasher.hash(x : U) => V
   def hashKey : hasher#V
}

trait ProductReader[hasher <: Hash, product <: Product[hasher]] { 
   def getByKey(key : hasher#U) : product
   def getByHashed(hash : hasher#V) : product
}

Now, with your sample usage :

object MyHash extends Hash {
  type U = String
  type V = Long
  def hash(key : String) : Long = key.toLong
}

class MyProduct(val hasher: MyHash.type) extends Product[MyHash.type] {
  def hashKey: Long = hasher.hash(key)

  val key: String = "MyProduct"
}

class MyProductReader extends ProductReader[MyHash.type, MyProduct] {
   def getByKey(key : String) : MyProduct = ???
   def getByHashed(hash : Long) : MyProduct = ???
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM