だから私はかなり基本的なnewtypeを持っています:
newtype SomeID = SomeID Word64 deriving (Show,Eq)
そして、ボックス化されていないベクトルでこの型を使用したいのですが、最初の検査では、これは導出よりも複雑なようです...たとえばStorable
.... そして、「派生」と言うときは、理想的には による自動派生を望んでいますライブラリGeneralizedNewtypeDeriving
によるデフォルト型の派生を見ると、これがわかります。vector
さらに、Web を検索すると、問題を解決するために使用することを提案しているこの 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
、型ファミリのロールに関するより長いエラー メッセージが表示されます。これは、歴史的にこの手法を実現する際の問題であったと思います。何か変わった?