简化问题
给定
class Foo f where
frobnicate :: f -> Float
我怎么能允许任何instance
人Foo
进入
data Bar = Bar { <here> }
?
实际问题
给定
-- Typically lightweight geometric objects, e.g. spheres
class Shape s where
intersect :: (RealFrac t, Floating t, Ord t, Shape s)
=> Ray t -> s t -> Maybe (DifferentialGeometry t)
和
-- Primitives contain higher level informations, like material properties
class Primitive p where
intersect :: (RealFrac t, Floating t, Ord t, Primitive p)
=> Ray t -> p t -> Maybe (Intersection t)
Primitive.intersect
注意和的签名的唯一区别Shape.intersect
在于返回类型。
现在我想添加一个包装器,它基本上将任何Shape
转换为Primitive
.
我认为它的工作原理大致是这样的:
data ShapeWrapperPrimitive t = ShapeWrapperPrimitive {
shape :: (Shape s) => s t
}
或者换句话说,我想添加一个任意shape
成员,该成员属于Shape
该类。
但是,这给了我Illegal polymorphic or qualified type
.