是否可以指定数据库的每个成员满足类型类型,从而隐含类约束?例如。Typeclass在数据类型的每个值上实例化
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
字典? - 但我在一天中看到了一些魔法。
我把它作为http://stackoverflow.com/questions/32408110/datakinds-and-type-class-instances的副本关闭,但重新打开它。单独的'Foo'类限制只能在'AB'上操作,可能会给我无法想象的魔法空间。 – Cirdec