1
給定以下代碼,從go
類型中刪除forall a r
將失敗,並顯示「重疊實例用於可鍵入(D r)」。我想知道爲什麼?當存在不定量方法時重疊實例錯誤的原因
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
module M where
import Data.Typeable (Proxy, Typeable, cast)
class C r where
data D r :: *
deriving instance Typeable D
data A = forall r . A (D r)
go :: forall r a . (Typeable a, Typeable (D r)) => a -> Proxy r -> A
go a _ = case cast a of
Just (b :: D r) -> A b
Nothing -> error "fail to cast"
錯誤還表示,「這一決定取決於r
實例化」 - 但不是由提供Proxy r
寄託?
謝謝!如果發出警告「你使用了一個同名的類型變量,你打算添加嗎?」確實很好。 – ron
@ron我強烈同意。我甚至會更改Haskell定義,以便在默認情況下啓用作用域類型變量,無論有沒有。這會破壞兼容性,但我真的很想知道這會破壞多少(我不知情的猜測是非常少的)。 – chi