一个类中的多个类型同义词

姆沃姆巴特

有没有办法根据关联的类型同义词定义类型同义词?(不确定我是否有正确的术语。)

{-# 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短期的,但它可以比变得更加复杂。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章