我想定义一个仿函数实例为以下类别:scalaz'Functor怎样才能给上下限绑定的更高类型?
class RequiresManifest[A: Manifest] {
def value: A
}
class RequiresAnyRef[A <: AnyRef] {
def value: A
}
class RequiresBothManifestAndAnyRef[A <: AnyRef: Manifest] {
def value: A
}
这可能吗?或者可以定义'BoundedFunctor特质?是这样的:
trait BoundedFunctor[F[_], Bound[_]] {
def fmap[A: Bound, B: Bound](r: F[A], f: A => B): F[B]
}
这里是我的激励例如:如何定义的类TypedConverter函子。
import com.thoughtworks.xstream.converters.Converter
abstract class TypedConverter[A <: AnyRef: Manifest] extends Converter {
final def canConvert(klass: Class[_]) =
manifest[A].erasure.isAssignableFrom(klass)
final def marshal(value: AnyRef, writer: HierarchicalStreamWriter,
context: MarshallingContext) =
typedMarshal(value.asInstanceOf[A], writer, context)
final def unmarshal(reader: HierarchicalStreamReader,
context: UnmarshallingContext) =
typedUnmarshal(reader, context)
def typedMarshal(value: A, writer: HierarchicalStreamWriter,
context: MarshallingContext): Unit
def typedUnmarshal(reader: HierarchicalStreamReader,
context: UnmarshallingContext): A
}
这-kinded更高类型在其类型参数两个约束,第一清单,因为这是在“canConvert”,第二AnyRef的实现中使用的,因为解组需要对象。
其实我试图创建一个InvariantFunctor,但是Functor会做。
我明白了。但有一点对我来说还不清楚:应该怎么使用这个Functor?你能举几个例子说明什么应该工作,哪些不应该工作? – Christian
BoundedFunctor似乎为第一种情况做了诀窍,但我猜想剩下的部分需要一些隐含的jiggerypokery。 – Stacy