1

所以我有一个非常基本的新类型:

newtype SomeID = SomeID Word64 deriving (Show,Eq)

而且我想在未装箱的向量中使用这种类型,但是在第一次检查时,这似乎比推导...说...更复杂Storable。当我说“派生”时,理想情况下,我希望通过自动派生。GeneralizedNewtypeDeriving当我查看vector库对默认类型的派生时,我看到了这个

此外,在搜索网络时,我遇到了这篇建议使用来解决问题的SO 帖子。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,我会收到关于类型家族角色的更长的错误消息,我假设这在历史上一直是使这种技术成为现实的问题。有什么改变吗?

4

0 回答 0