考虑以下定义:
class Foo a where
foo :: a -> Int
class Bar a where
bar :: a -> [Int]
Run Code Online (Sandbox Code Playgroud)
现在,我怎么说在Haskell中"每个Foo都是a Bar,bar默认定义为bar x = [foo x]"?
(无论我尝试什么,编译器都给我"非法实例声明"或"约束不小于实例头")
顺便说一句,我可以通过其他方式定义我的Foo和Bar类,如果这会有所帮助.
max*_*kin 11
class Foo a where
foo :: a -> Int
-- 'a' belongs to 'Bar' only if it belongs to 'Foo' also
class Foo a => Bar a where
bar :: a -> [Int]
bar x = [foo x] -- yes, you can specify default implementation
instance Foo Char where
foo _ = 0
-- instance with default 'bar' implementation
instance Bar Char
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1844 次 |
| 最近记录: |