是否有可能知道(在编译时)编译器在实例重叠时选择了哪个实例?
Contrived example,
{-# LANGUAGE IncoherentInstances #-}
class Transformable a b where
transform :: a -> b
instance Transformable a Int where
transform _ = 17
instance a ~ b => Transformable a b where
transform x = x
tranform (1:: Int) :: Int
的结果是什么? (1或17)
如果编译器实例1或实例2,有没有办法知道何时实例化Transformable Int Int
?
当没有重叠但是实例链很复杂时,它也有时很有用。
答案 0 :(得分:1)
您始终可以使用Debug.Trace
:
import Debug.Trace
...
instance Transformable a Int where
transform _ = trace "using Int definition" 17
instance a ~ b => Transformable a b where
transform x = trace "using a ~ b definition" x