可比性在榆树中意味着什么? [英] What does comparable mean in Elm?
问题描述
我很难理解榆树中可比的
到底是什么。榆树似乎和我一样困惑。
I'm having trouble understanding what exactly a comparable
is in Elm. Elm seems as confused as I am.
在REPL上:
> f1 = (<)
<function> : comparable -> comparable -> Bool
所以 f1
接受可比对象。
> "a"
"a" : String
> f1 "a" "b"
True : Bool
所以看起来 String
是可比较的。
So it seems String
is comparable.
> f2 = (<) 1
<function> : comparable -> Bool
所以 f2
接受可比较的内容。 / p>
So f2
accepts a comparable.
> f2 "a"
As I infer the type of values flowing through your program, I see a conflict
between these two types:
comparable
String
所以字符串
是和不具有可比性吗?
为什么 f2
的类型不是 number- >布尔
? f2
还能接受什么其他可比对象?
So String
is and is not comparable?
Why is the type of f2
not number -> Bool
? What other comparables can f2
accept?
推荐答案
通常,当您看到Elm中某个类型中的type变量,此变量不受限制。然后,当您提供某种特定类型的东西时,该变量将被该特定类型替换:
Normally when you see a type variable in a type in Elm, this variable is unconstrained. When you then supply something of a specific type, the variable gets replaced by that specific type:
-- says you have a function:
foo : a -> a -> a -> Int
-- then once you give an value with an actual type to foo, all occurences of `a` are replaced by that type:
value : Float
foo value : Float -> Float -> Int
可比
是类型变量,具有内置特殊含义。意思是说,它将仅与可比较类型匹配,例如 Int
, String
和其他一些类型。但除此之外,它的行为应相同。因此,我认为类型系统中存在一个小错误,因为您得到了:
comparable
is a type variable with a built-in special meaning. That meaning is that it will only match against "comparable" types, like Int
, String
and a few others. But otherwise it should behave the same. So I think there is a little bug in the type system, given that you get:
> f2 "a"
As I infer the type of values flowing through your program, I see a conflict
between these two types:
comparable
String
如果错误不存在,您将得到:
If the bug weren't there, you would get:
> f2 "a"
As I infer the type of values flowing through your program, I see a conflict
between these two types:
Int
String
编辑:我打开了问题针对此错误
I opened an issue for this bug
这篇关于可比性在榆树中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!