是否有一种方法可以根据关联的类型同义词定义类型同义词?(不确定我是否有正确的术语)
{-# LANGUAGE TypeFamilies #-}
class Reproductive a where
-- | A sequence of genetic information for an agent.
type Strand a
-- | Full set (both strands) of genetic information for an organism.
type Genome a = (Strand a, Strand a)
这是我得到的错误信息。
λ> :l stackOverflow.hs
[1 of 1] Compiling Main ( stackOverflow.hs, interpreted )
stackOverflow.hs:9:8: error:
‘Genome’ is not a (visible) associated type of class ‘Reproductive’
|
9 | type Genome a = (Strand a, Strand a)
| ^^^^^^
Failed, no modules loaded.
我可以在任何地方使用(Strand a, Strand a)
,但使用Genome a
会更好。
可以将类型同义词与类分开定义:
{-# LANGUAGE TypeFamilies #-}
-- | Full set (both strands) of genetic information for an organism.
type Genome a = (Strand a, Strand a)
class Reproductive a where
-- | A sequence of genetic information for an agent.
type Strand a
或者,如果你想在某些实例中覆盖它,那么你可以这样定义它:
{-# LANGUAGE TypeFamilies #-}
class Reproductive a where
-- | A sequence of genetic information for an agent.
type Strand a
-- | Full set (both strands) of genetic information for an organism.
type Genome a
type Genome a = (Strand a, Strand a)
这似乎是多余的,但是您可以将第一个type Genome a
视为签名,第二行视为默认实现。在本例中,签名仅为type Genome a :: *
或type Genome a
,但它可能比这更复杂。