class Hello a where
method1 :: a -> String
method1 a = "Hello"
data World s = World [s]
instance Hello a => World a where
method1 a = "Hello World"
最佳答案
instance
没有多大意义。在类型上定义一个实例。可能的解决方法是:
instance Hello (World a) where
method1 _ = "Hello World"
instance
处理指定类型(或类型列表)在类上的映射方式,因此,如果您有带有参数C
的类a
,则可以指定:class C a where
foo :: a -> a
a
实际上适用于C
。使用instance
,您可以指定给定类型(这些类型可以是“泛型”)适用于C
,因此接下来您可以说:instance C a where
foo = id
a
(因此所有类型)都是C
的实例。并且该foo
应该被视为id
函数。a
施加其他约束,例如a
应该实例化另一个类。例如与instance (Integral i) => C i where
foo = (+) 1
i
都是C
的实例,其中foo
被定义为增量函数。 请注意,您不能简单地将此实例与上一个实例结合起来,因为Haskell不知道在这种情况下该选择哪一个。 instance (Hello a) => Hello (World a) where
method1 _ = "Hello World"
https://stackoverflow.com/questions/29036066/
相关文章:
asp.net - 错误: SignalRRouteExtension.Mapconnection
compiler-errors - ifort composer_xe_2015.3.187的编译问
compiler-errors - 由于 glib 错误,无法处理 gobject-introspe
scala - 使用 Deadbolt 2 : overriding method getSubje
compiler-errors - 在Solaris Sparc 11下强制使用Sun Studio
jsf - ServletContext.TEMPDIR无法解析或不是字段