是否可以指定數據庫的每個成員滿足類型類型,從而隱含類約束?例如。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