PureScriptにReduxのようなストアを書き込もうとしています。このタイプのクラスインスタンスが統合に失敗したのはなぜですか?
私はAction
型クラスを定義し、各アクションの代数データ型をより小さなモジュールに分割しました。
class Action a
data FooAction
= UpdateFoo String
| ResetFoo
data BarAction
= UpdateBar Int
| ResetBar
data ResetAll = ResetAll
instance fooAction :: Action FooAction
instance barAction :: Action BarAction
いくつかの状態タイプと更新関数を定義しました。更新機能は、すべてのタイプのアクションを受け取ることがあります。
newtype Foo = Foo String
newtype Bar = Bar Int
updateFoo :: forall a. (Action a) => a -> Foo -> Foo
updateFoo a foo =
case a of
UpdateFoo str -> Foo str
ResetFoo -> Foo ""
ResetAll -> Foo ""
_ -> foo
updateBar :: forall a. (Action a) => a -> Bar -> Bar
updateBar a bar =
case a of
UpdateBar num -> Bar num
ResetBar -> Bar 0
ResetAll -> Bar 0
_ -> bar
しかし、このコードはTypesDoNotUnifyエラーを生成します。
Could not match type
FooAction
with type
a0
while checking that expression case a of
(UpdateFoo str) -> Foo str
ResetFoo -> Foo ""
ResetAll -> Foo ""
_ -> foo
has type Foo
in value declaration updateFoo
where a0 is a rigid type variable
このエラーはなぜ発生しますか?このような更新機能はどのように実装すればよいですか?
ありがとうございました!型クラス関数とパターンマッチングを使用します。 – katashin