Type constraints in instance declaration but not class
我想创建一个具有类型约束的函数定义的实例,但不想将类型约束添加到类中。
1 2 3 4 5 6 7 8 9 |
如何指定这是包含
这项工作:
1 2 3 4 5 6 7 8 9 10 |
号
但我不想为每种类型的
我试过这个:
1 2 3 4 5 6 7 8 9 |
但我得到一个错误:
1
2
3
4
5
6
7
8
9
10
11 Couldn't match type ‘b’ with ‘b1’
‘b’ is a rigid type variable bound by
the instance declaration at Test.hs:31:10
‘b1’ is a rigid type variable bound by
the type signature for f :: Maybe b -> b1 at Test.hs:32:3
Expected type: Maybe b -> b1
Actual type: Maybe b1 -> b1
Relevant bindings include
f :: Maybe b -> b1 (bound at Test.hs:32:3)
In the expression: fMaybe
In an equation for ‘f’: f = fMaybe
号
不更改类或类型是不可能的。
MultiParamTypeClasses 方法实际上并不要求您为所有Num 类型编写单独的实例—以下工作:不过,我认为这不是一个特别好的方法。
您可以使用
ConstraintKinds 允许每个实例有选择地约束包含的类型。1
2
3
4
5
6
7
8
9
10
11
12号
您可以切换到最初只允许包含
Num 类型的类型。1
2
3
4
5然后,
1
2
3
4
5
6class Foo a where
f :: a b -> b
instance Foo NMaybe where
f (NJust i) = i+1
f (NNothing) = 0。