與monad-coroutine包的工作,我有一些協同程序做了很多工作,它需要一些投入,不時:提高協程請求類型安全
Coroutine (Request SomeRequest SomeResponse) (State MyState) a
其中
data SomeRequest
= GetImportantData Int
| OtherImportantStuff Float
| SomethingElse (Vector Int)
| ...
data SomeResponse
= ImprtantData (Vector Float)
| ImportantStuff Int
| ...
正如你所看到的對於SomeRequest
中的每個條目,我都有相應的條目SomeResponse
。
在這個協同程序的運行時我有這樣的事情:
...
ImportantData info <- request (GetImportantData 666)
...
現在恐怕這種做法並不好,因爲我要的是確保每當我請求與GetImportantData
重要數據唯一可能的迴應是ImportantData
,沒有別的。使用我目前的方法,我必須在每次發出請求時都進行模式匹配(以確保輸入實際上是我想要的)。
什麼辦法可以提高設計/方法,以確保爲GetImportantData
我得到ImportantData
回來只爲OtherImportantStuff
我得到ImportantStuff
只有等?
將每個構造函數轉換爲它自己的類型,並使用帶有相關數據或類型家族。 – user2407038