次のクラスの 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 クラスの Functor を定義するにはどうすればよいですか。
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
}
この上位種類の型には、その型パラメーターに 2 つの制約があります。1 つ目は「canConvert」の実装で使用されるための Manifest、2 つ目の AnyRef は非整列化にオブジェクトが必要であるためです。
実際には InvariantFunctor を作成しようとしていますが、最初は Functor で十分です。