So I've got a pretty basic newtype:
newtype SomeID = SomeID Word64 deriving (Show,Eq)
And I would like to use this type in an unboxed vector, but on first inspection this seems to be more complicated than deriving...say...Storable
. And when I say "deriving", I'm ideally hoping for automatic derivation via GeneralizedNewtypeDeriving
When I look at the derivation of the default types by the vector
library, I see this.
Furthermore, when searching the web, I ran across this SO post that was suggesting using Template Haskell
to solve the problem. I'm currently not using TH and it would be nice to not to be forced down that road.
All I'm trying to do here is have an otherwise unboxable data type that happens to be semantically orthogonal to that original type because I want to use a smart constructor in my API. Is there any way to make this type unboxable without resorting to Template Haskell
or a ton of boiler plate? This seems like something that could be generically derived given newtype
will just be erased at compile time. Ideally, I would like to just do this:
{-# 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)
Right now, when I try to automatically derive using the method above, I get this error:
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)
If I use GeneralizedNewtypeDeriving
instead of DeriveAnyClass
, I get a much longer error message about type family roles, which I'm assuming has historically been the issue with making this technique a reality. Has anything changed?