当我在构造函数中创建一个带有 Symbol 的新类型并打开 IncoherentInstances 时,只有在编译时填充符号时才会选择该类型的正确实例......
{-# LANGUAGE DataKinds, GADTs, KindSignatures, FlexibleInstances, IncoherentInstances #-}
import GHC.TypeLits
data Object:: Symbol -> * where
Object :: Object sy
instance Show (Object "dog") where
show _ = "dog"
instance Show (Object x) where
show _ = "other"
main = do
let name = "dog"
print (undefined :: Object "dog") -- outputs "dog", as expected
print (undefined :: Object "cat") -- outputs "other", as expected
print (undefined :: Object name) -- outputs "other", I expected "dog"
有没有办法在运行时提供字符串符号值?如果不允许这样做,为什么还要编译(即,如果除了默认情况之外没有解决任何问题,那么任何人何时甚至想使用第三次打印中的分配?)