我正在尝试在 Haskell 中编写一个简单的光线追踪器。我想定义一个表示各种可用表面的类型类,并使用一个函数来确定光线与它们相交的位置:
{-# LANGUAGE RankNTypes #-}
data Vector = Vector Double Double Double
data Ray = Ray Vector Vector
class Surface s where
intersections :: s -> Ray -> [Vector]
-- Obviously there would be some concrete surface implementations here...
data Renderable = Renderable
{ surface :: (Surface s) => s
, otherStuff :: Int
}
getRenderableIntersections :: Renderable -> Ray -> [Vector]
getRenderableIntersections re ra = intersections (surface re) ra
然而,这给了我错误:
Ambiguous type variable 's' in the constraint:
'Surface'
arising from a use of 'surface'
(实际的代码更复杂,但我试图将它提炼成更简单的东西,同时保持我想要实现的要点)。
我该如何解决?或者,鉴于我来自标准的 OO 背景,我从根本上做错了什么?