2

我需要一个异构的类型安全的容器来存储不相关的类型 A、B、C。

这是一种类型级别的规范:

trait Container {
  putA(a: A) 
  putB(b: B)
  putC(c: C)
  put(o: Any) = { o match {
    case a: A => putA(a)
    case b: B => putB(b)
    case c: C => putC(c)
  }
  getAllAs : Seq[A]
  getAllBs : Seq[B]
  getAllCs : Seq[C]
}

哪种类型最适合支持此容器?

是否值得为 A、B、C 类型创建一个 Containerable[T] 类型类?

谢谢。

4

4 回答 4

7

正如其他人所建议的那样,您可以利用无形的Coproduct类型。这是一个例子。

// let's define a Coproduct of the two types you want to support
type IS = Int :+: String :+: CNil

// now let's have a few instances
val i = Coproduct[IS](42)
val i2 = Coproduct[IS](43)
val s = Coproduct[IS]("foo")
val s2 = Coproduct[IS]("bar")

// let's put them in a container
val cont = List(i, s, i2, s2)

// now, do you want all the ints?
val ints = cont.map(_.select[Int]).flatten

// or all the strings?
val strings = cont.map(_.select[String]).flatten

// and of course you can add elements (it's a List)
val cont2 = Coproduct[IS](12) :: cont
val cont3 = Coproduct[IS]("baz") :: cont2

现在这当然不是通用容器最直观的 API,但可以使用Coproduct表示多种类型的 a 轻松地将逻辑封装在自定义类中。

这是一个实现的草图

import shapeless._; import ops.coproduct._

class Container[T <: Coproduct] private (underlying: List[T]) {
  def ::[A](a: A)(implicit ev: Inject[T, A]) =
    new Container(Coproduct[T](a) :: underlying)

  def get[A](implicit ev: Selector[T, A]) =
    underlying.map(_.select[A]).flatten

  override def toString = underlying.toString
}

object Container {
  def empty[T <: Coproduct] = new Container(List[T]())
}

例子

scala> type IS = Int :+: String :+: CNil
defined type alias IS

scala> val cont = 42 :: "foo" :: "bar" :: 43 :: Container.empty[IS]
cont: Container[IS] = List(42, foo, bar, 43)

scala> cont.get[Int]
res0: List[Int] = List(42, 43)

scala> cont.get[String]
res1: List[String] = List(foo, bar)
于 2014-10-26T14:27:19.713 回答
2

Miles Sabin 写了一篇关于未装箱联合类型的帖子;这在他的无形库中作为CoProduct实现:

shapeless 有一个Coproduct类型,是 ScalaEither对任意数量选择的概括

我绝对不是 shapeless 方面的专家,但是如果您使用 shapeless 标签创建新问题或编辑您的问题,那么您可以获得使用所需的任何帮助CoProduct

于 2014-10-25T20:37:30.877 回答
1

你应该看看 Shapeless 的 HList 或 Coproduct;我不会自己重新发明这个。

于 2014-10-25T23:10:06.660 回答
0

这是第一个版本,但我想对 type 进行抽象:

trait Container {

  def putInt(i: Int)
  def putString(s: String)
  def put(o: Any) = o match {
    case i: Int => putInt(i)
    case s: String => putString(s)
  }
  def getInts() : Seq[Int]
  def getStrings() : Seq[String]

}

class MutableContainer extends Container {

  val ints = mutable.ArrayBuffer[Int]()
  val strings = mutable.ArrayBuffer[String]()

  override def putInt(i: Int): Unit = ints += i

  override def putString(s: String): Unit = strings += s

  override def getStrings(): Seq[String] = strings

  override def getInts(): Seq[Int] = ints

}

object TestContainer extends App {
  val mc = new MutableContainer()
  mc.put("a")
  mc.put("b")
  mc.put(1)
  println(mc.getInts())
  println(mc.getStrings())
}

现在尝试抽象类型

trait Container {
  def getInts() : Seq[Int]
  def getStrings() : Seq[String]

  def put[T](t: T)
  //def get[T] : Seq[T]
}

class MutableContainer extends Container {
  val entities = new mutable.HashMap[Class[_], mutable.Set[Any]]() with mutable.MultiMap[Class[_], Any]

  override def getStrings(): Seq[String] = entities.get(classOf[String]).map(_.toSeq).getOrElse(Seq.empty).asInstanceOf[Seq[String]] //strings
  override def getInts(): Seq[Int] = entities.get(classOf[Int]).map(_.toSeq).getOrElse(Seq.empty).asInstanceOf[Seq[Int]]

  //override def get[T]: Seq[T] = entities.get(classOf[T]).map(_.toSeq).getOrElse(Seq.empty).asInstanceOf[Seq[T]]
  override def put[T](t: T): Unit = entities.addBinding(t.getClass, t)
}

trait Containable[T] {
  def typ : String
}

trait Cont {
  implicit object IntContainable extends Containable[Int] {
    override def typ: String = "Int"
  }
  implicit object StringContainable extends Containable[String] {
    override def typ: String = "String"
  }
}

object TestContainer extends App {
  val mc = new MutableContainer()
  mc.put("a")
  mc.put("b")
  mc.put(1)
  println(mc.getInts())
  println(mc.getStrings())
  println(mc.entities.keys)
}

但是我遇到了 java.lang.Integer 和 Int 的问题……</p>

于 2014-10-25T20:07:24.800 回答