绕过模式匹配中的类型擦除
我正在尝试解决模式匹配中的类型擦除问题。假设:
import java.io._
trait Serializer[V] {
def save(os: OutputStream, v: V): Unit
def load(in: InputStream): V
}
trait HasSerializer[V] { def serializer: Serializer[V] }
如何在没有警告且没有 asInstanceOf
: 的情况下编译它
def test[V](os: OutputStream, v: V): Unit = v match {
case hs: HasSerializer[V] => hs.serializer.save(os, v)
case _ => ???
}
? test
使用映射中的值进行调用,并且无法提供类清单。
也许有什么奇特的提取技巧?
I am trying to work around a type erasure in pattern matching. Assuming:
import java.io._
trait Serializer[V] {
def save(os: OutputStream, v: V): Unit
def load(in: InputStream): V
}
trait HasSerializer[V] { def serializer: Serializer[V] }
How can I get this to compile without warning and without asInstanceOf
:
def test[V](os: OutputStream, v: V): Unit = v match {
case hs: HasSerializer[V] => hs.serializer.save(os, v)
case _ => ???
}
? test
is called with a value from a map and there is no means to provide a class manifest.
Any fancy extractor trick maybe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以将 Serializer 改为抽象类,则可以为其提供 Manifest 作为隐式构造函数参数,并使用它在构造时获取具体类,然后使用它进行动态类型检查。
If you can make Serializer an abstract class instead, you can give it a Manifest as an implicit constructor parameter, and use that to get the concrete class at construction, then use it later for dynamic type checks.
好吧,这个问题有一个错误的前提条件(正如我刚刚意识到的那样)——我们可以将
Serializer
分解为序列化器和反序列化器。显然,当我有一个V
实例时,我的用例是序列化,并且不需要V
作为返回类型。因此就足够了,任何类型都可以混合它。并且这样做:
对于反序列化,我们要么提供反序列化器以及
Ref[V]
的构造,要么依赖于通过 <代码>ObjectInputStream。Well the question has kind of a wrong precondition (as I just realize) -- we can take
Serializer
apart into a serializer and a deserializer. Obviously, when I have an instance ofV
, my use case is serialization, and that doesn't requireV
as a return type. Thuswould suffice, and any type can mix that in. And do:
And for deserialization, we would either provide the deserializer along with the construction of a
Ref[V]
, or rely on class lookup throughObjectInputStream
.