是否可以指定数据种类的每个成员都满足类型类,从而隐含类约束?例如
data AB = A | B
class Foo (a :: AB) where get :: proxy a -> String
instance Foo A where get _ = "A"
instance Foo B where get _ = "B"
-- note lack of constraint here
get' :: proxy (a :: AB) -> String
get' = get
基本上a
是一个AB
,所以我们确定它有一个实例Foo
。我觉得不太可能——从哪里得到Foo
字典?- 但我在我的日子里看到了一些魔法。