[英]Broken code with roles in GHC 7.8
我的一些代碼被最新版本的ghc 7.8.2打破了。
我正在使用GeneralizedNewtypeDeriving
使用以下方法派生Data.Vector.Unbox
實例:
data VoxelPos = VoxelPos
{-# UNPACK #-} !Int
{-# UNPACK #-} !Int
{-# UNPACK #-} !Int
deriving (Show, Eq, Ord)
newtype FacePos = FacePos VoxelPos deriving ( Eq, Hashable, NFData, G.Vector U.Vector, M.MVector U.MVector, U.Unbox)
其中VoxelPos
使用(Int, Int, Int)
手動滾動實例:
newtype instance U.MVector s VoxelPos = MV_VoxelPos (U.MVector s (Int, Int, Int))
newtype instance U.Vector VoxelPos = V_VoxelPos (U.Vector (Int, Int, Int))
instance U.Unbox VoxelPos
instance M.MVector U.MVector VoxelPos where
basicLength (MV_VoxelPos v) ...
...
這與之前版本的ghc一起使用。 但升級ghc后,我收到以下錯誤:
Could not coerce from ‘U.MVector s (Int, Int, Int)’ to ‘U.MVector
s FacePos’
because the second type argument of ‘U.MVector’ has role Nominal,
but the arguments ‘(Int, Int, Int)’ and ‘FacePos’ differ
arising from the coercion of the method ‘M.basicLength’ from type
‘forall s. U.MVector s VoxelPos -> Int’ to type
‘forall s. U.MVector s FacePos -> Int’
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (M.MVector U.MVector FacePos)
我認為,這是因為增加了角色。 我知道使用GeneralizedNewtypeDeriving
時角色可以提高安全性,當然,這非常好!
有哪些解決方案可以解決這個問題? 什么是最推薦的?
這里有一個錯誤是明智的-它有可能是U.MVector
例如FacePos
是完全無關的實例VoxelPos
。 但是有一種很好的解決方法:
newtype instance U.MVector s FacePos = MV_FacePos (U.MVector s VoxelPos)
這應該擺脫你所看到的特定錯誤。
但是,我認為你會立即遇到另一個與角色相關的錯誤,因為其他函數(不是basicLength
,你被basicLength
地方)以角色目前無法處理的方式使用MVector
參數。
GHC團隊意識到了這個問題並正在努力:請參閱https://ghc.haskell.org/trac/ghc/ticket/9112和https://ghc.haskell.org/trac/ghc/ticket/9123
與此同時,我擔心我的唯一建議是使用unsafeCoerce
。 :(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.