Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell:类型类:多重继承示例_Haskell_Inheritance - Fatal编程技术网

Haskell:类型类:多重继承示例

Haskell:类型类:多重继承示例,haskell,inheritance,Haskell,Inheritance,我知道我们可以使用类型类实现多重继承。我编写了小的haskell代码,但无法解决问题 {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE StandaloneDeriving #-} class (Eq a, Show a) => C a where getHashCode :: a -> Integer getHashCode obj =

我知道我们可以使用类型类实现多重继承。我编写了小的haskell代码,但无法解决问题

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE StandaloneDeriving #-}

class (Eq a, Show a) => C a where
    getHashCode :: a -> Integer
    getHashCode obj = 123


type Id = Int
type Name = String

data Employee = Employee Id Name deriving C
当我试图加载上面的代码时,我得到了以下错误。有什么帮助吗

 No instance for (Eq Employee)
      arising from the 'deriving' clause of a data type declaration
   Possible fix:
      use a standalone 'deriving instance' declaration,
        so you can specify the instance context yourself
    When deriving the instance for (C Employee)
Failed, modules loaded: none.
我在谷歌上搜索了一段时间,但找不到多重继承的好例子。如果您提供一些信息,例如Haskell中的多重继承,将会很有帮助

参考资料:

class (Eq a, Show a) => C a where
并不意味着实现
C
的类型自动实现
Eq
Show
,而是意味着它们必须首先实现
Eq
Show
,然后才能实现
C

Haskell中的
与Java中的
也不一样,它更接近接口,但不能以相同的方式使用(也不应该)。Haskell实际上没有OOP意义上的继承或类的概念,因为它不是OOP语言


但是,如果您想让类型的
Eq
自动显示
实例,只需将它们添加到数据类型的
派生
子句中即可。

谢谢您的回答。我们可以派生多个类型类,但我的问题是我想使用“class(Eq a,Show a)=>ca where”语句,这样我如何解决错误。