是否有辦法用相關的型別同義詞來定義一個型別同義詞?(不確定我是否有正確的術語。)
{-# LANGUAGE TypeFamilies #-}
class Reproductive a where<
--|一個代理人的遺傳資訊序列。
type Strand a
--|一個生物體的全套(兩股)遺傳資訊。
type Genome a = (Strand a, Strand a)
下面是我得到的錯誤資訊。
λ> :l stackOverflow.hs
[1 of 1] Compiling Main ( stackOverflow.hs, interpreted )
stackOverflow.hs:9:8: error:
'Genome'不是一個(可見的)相關的type的類'Reproductive'。
|
9 | type Genome a = (Strand a, Strand a)?
| ^^^^^^
Failed, no modules loaded.
我可以隨處使用(Strand a, Strand a),但如果能使用基因組a就更好了。
uj5u.com熱心網友回復:
你可以從類中單獨定義型別的同義詞:
{-# LANGUAGE TypeFamilies #-}
--|一個生物體的全套(兩股)遺傳資訊。
type Genome a = (Strand a, Strand a)
class Reproductive a where<
--|一個代理人的遺傳資訊序列。
type Strand a
另外,如果你想在某些情況下能夠覆寫它,那么你可以這樣定義它:
。{-# LANGUAGE TypeFamilies #-}
class Reproductive a where<
--|一個代理人的遺傳資訊序列。
type Strand a
--|一個生物體的全套(兩股)遺傳資訊。
type Genome a
type Genome a = (Strand a, Strand a)
這似乎是多余的,但你可以把第一行type Genome a看作是簽名,第二行是默認的實作。在這種情況下,簽名是簡單的type Genome a :: *或者簡稱為type Genome a,但是它可以變得比這更復雜。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/316966.html
標籤:
