通过独立导出导出
Deriving Via With Standalone Deriving
我不太确定我做错了什么:
data Vector2D u = Vector2D {
_x :: u,
_y :: u
} deriving stock (Show, Eq, Functor, Foldable, Traversable)
{-# INLINE addVector2 #-}
addVector2 :: (Additive a) => Vector2D a -> Vector2D a -> Vector2D a
addVector2 (Vector2D { _x = x1, _y = y1 }) (Vector2D { _x = x2, _y = y2 }) =
Vector2D { _x = x1 + x2, _y = y1 + y2 }
instance (Additive a) => Additive (Vector2D a) where
(+) = addVector2
newtype Square a = Square {
unpackSquare :: Vector2D a
} deriving stock (Show, Eq)
到目前为止,一切正常(Additive 在 algebra 包中定义,但非常不言自明)。
但是,现在我想聪明地使用 DerivingVia 和 StandaloneDeriving,但我什至无法编译下一行
deriving instance (Additive a) => Additive (Square a) via (Vector2D a)
但这让我感动
* Expected kind `k0 -> * -> Constraint',
but `Additive (Square a)' has kind `Constraint'
* In the stand-alone deriving instance for
`(Additive a) => Additive (Square a) via (Vector2D a)'
谁能告诉我我做错了什么?我是 运行 GHC 8.6.2
是
deriving via (Vector2D a) instance (Additive a) => Additive (Square a)
你写的方式,via
看起来像一个类型变量
deriving instance (Additive a) => Additive (Square a) via (Vector2D a)
-- <==>
deriving instance forall a via. (Additive a) => Additive (Square a) (via) (Vector2D a)
这会产生种类不匹配错误,因为 Additive (Square a) :: Constraint
已经饱和,但您已将其应用于另外两个参数。
这是 shorthand 形式的倒退:
data T' = ...
deriving Class via T
我不太确定我做错了什么:
data Vector2D u = Vector2D {
_x :: u,
_y :: u
} deriving stock (Show, Eq, Functor, Foldable, Traversable)
{-# INLINE addVector2 #-}
addVector2 :: (Additive a) => Vector2D a -> Vector2D a -> Vector2D a
addVector2 (Vector2D { _x = x1, _y = y1 }) (Vector2D { _x = x2, _y = y2 }) =
Vector2D { _x = x1 + x2, _y = y1 + y2 }
instance (Additive a) => Additive (Vector2D a) where
(+) = addVector2
newtype Square a = Square {
unpackSquare :: Vector2D a
} deriving stock (Show, Eq)
到目前为止,一切正常(Additive 在 algebra 包中定义,但非常不言自明)。
但是,现在我想聪明地使用 DerivingVia 和 StandaloneDeriving,但我什至无法编译下一行
deriving instance (Additive a) => Additive (Square a) via (Vector2D a)
但这让我感动
* Expected kind `k0 -> * -> Constraint', but `Additive (Square a)' has kind `Constraint' * In the stand-alone deriving instance for `(Additive a) => Additive (Square a) via (Vector2D a)'
谁能告诉我我做错了什么?我是 运行 GHC 8.6.2
是
deriving via (Vector2D a) instance (Additive a) => Additive (Square a)
你写的方式,via
看起来像一个类型变量
deriving instance (Additive a) => Additive (Square a) via (Vector2D a)
-- <==>
deriving instance forall a via. (Additive a) => Additive (Square a) (via) (Vector2D a)
这会产生种类不匹配错误,因为 Additive (Square a) :: Constraint
已经饱和,但您已将其应用于另外两个参数。
这是 shorthand 形式的倒退:
data T' = ...
deriving Class via T