我无法理解comparable榆树究竟是什么.榆树似乎和我一样困惑.
在REPL上:
> f1 = (<)
<function> : comparable -> comparable -> Bool
Run Code Online (Sandbox Code Playgroud)
所以f1接受可比较的.
> "a"
"a" : String
> f1 "a" "b"
True : Bool
Run Code Online (Sandbox Code Playgroud)
所以它似乎String具有可比性.
> f2 = (<) 1
<function> : comparable -> Bool
Run Code Online (Sandbox Code Playgroud)
所以f2接受可比的.
> f2 "a"
As I infer the type of values flowing through your program, I see a conflict
between these two types:
comparable
String
Run Code Online (Sandbox Code Playgroud)
所以,String 是和是不是媲美?
为什么类型f2不number -> Bool?还有哪些其他可比数据可以f2接受?
通常,当您在Elm中看到类型变量时,此变量不受约束.然后,当您提供特定类型的某些内容时,该变量将被该特定类型替换:
-- 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
Run Code Online (Sandbox Code Playgroud)
comparable是一个具有内置特殊含义的类型变量.这意味着它只会与"可比"类型匹配,例如Int,String和其他一些类型.但否则它应该表现相同.所以我觉得类型系统中有一个小错误,因为你得到:
> f2 "a"
As I infer the type of values flowing through your program, I see a conflict
between these two types:
comparable
String
Run Code Online (Sandbox Code Playgroud)
如果没有bug,你会得到:
> f2 "a"
As I infer the type of values flowing through your program, I see a conflict
between these two types:
Int
String
Run Code Online (Sandbox Code Playgroud)
编辑:我为这个错误打开了一个问题
更新:感谢@charlie 输入,我包含了这个:
比较任意两个可比较的值。可比值包括
String、Char、Int、Float、Time或list或tuple包含可比值。这些也是唯一可用作 Dict 键或 Set 成员的值。
取自榆树文档: -这里
旧版本:
可比较的类型包括numbers、characters、strings、~~lists of comparable things和tuples of comparable things。请注意,具有 7 个或更多元素的元组不可比较;为什么你的元组这么大?
这意味着:
[(1,"string"), (2, "another string")]: List (Int, String)-是可比的
但是有
(1, "string", True):(Int, String, Bool)或
[(1,True), (2, False)]: List (Int, Bool )- 还没有可比性。
这个问题在这里讨论
注:通常人们遇到的问题comparable类型,当他们尝试使用联合类型作为重点在快译通。
联合类型的标签和构造函数没有可比性。所以以下内容甚至无法编译。
type SomeUnion = One | Two | Three
Dict.fromList [ (One, "one related"), (Two, "two related") ] : Dict SomeUnion String
Run Code Online (Sandbox Code Playgroud)
通常,当您尝试这样做时,有一种更好的方法来处理您的数据结构。但是在这得到决定之前 -可以使用AllDict。
| 归档时间: |
|
| 查看次数: |
3706 次 |
| 最近记录: |