所以我有一个非常基本的新类型:
newtype SomeID = SomeID Word64 deriving (Show,Eq)
我想在一个未装箱的矢量中使用这种类型,但是在第一次检查时,这似乎比导出...说... Storable
更复杂。当我说“推导”时,我理想地希望通过GeneralizedNewtypeDeriving
自动推导当我查看vector
库I see this推导出默认类型时。
此外,在搜索网络时,我遇到this SO post,建议使用Template Haskell
来解决问题。我目前没有使用TH,如果不被逼到那条路上会很好。
我在这里尝试做的就是有一个不可替换的数据类型,恰好在语义上与原始类型正交,因为我想在我的API中使用智能构造函数。 有没有办法让这种类型无法使用Template Haskell
或大量的锅炉板?这似乎可以归结为newtype
只会被删除在编译时。理想情况下,我想这样做:
{-# LANGUAGE DeriveAnyClass #-}
import qualified Data.Vector.Generic.Mutable as M
import qualified Data.Vector.Generic as G
import Data.Vector.Unboxed
import Data.Word
newtype SomeID = SomeID Word64 deriving (Show,Eq,Unbox,M.MVector MVector,G.Vector Vector)
现在,当我尝试使用上述方法自动派生时,我收到此错误:
Var/Type length mismatch:
[a_a3zv]
[]
Var/Type length mismatch:
[a_a3zv]
[]
Var/Type length mismatch:
[a_a3zS]
[]
Var/Type length mismatch:
[a_a3zS]
[]
/home/oldmanmike/so-question/Main.hs:14:50:
No instance for (G.Vector Vector SomeID)
arising from the 'deriving' clause of a data type declaration
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (Unbox SomeID)
/home/oldmanmike/so-question/Main.hs:14:56:
No instance for (M.MVector Word64)
arising from the first field of ‘SomeID’ (type ‘Word64’)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (M.MVector SomeID)
/home/oldmanmike/so-question/Main.hs:14:75:
No instance for (G.Vector Word64)
arising from the first field of ‘SomeID’ (type ‘Word64’)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (G.Vector SomeID)
如果我使用GeneralizedNewtypeDeriving
代替DeriveAnyClass
,我会收到关于类型系列角色的更长时间的错误消息,我认为这在历史上一直是使这项技术成为现实的问题。有什么变化吗?