当Base被添加到下面的代码中时,为什么OCaml编译器期望h和w是ints?tup应该是一个元组-有语法问题吗?是什么关于Base导致了这个错误?
open Base
let () =
let tup = ("hello", "world") in
let h, w = tup in
if h = w then print_endline "equal" else print_endline "not equal"错误:
91 | if h = w then print_endline "equal" else print_endline "not equal"
^
Error: This expression has type string but an expression was expected of type
int发布于 2021-06-05 05:35:15
Base和其他Janestreet库一起,不鼓励多态比较函数,并隐藏compare、(=)和其他运算符以及它们的单调等效项。他们或多或少随意地选择了比较整数的函数(遵循SML传统)。
您仍然可以使用以下命令获取多态比较函数
open Poly或者,在本地,
let true = Poly.((1,2) = (1,2))发布于 2021-06-05 04:55:37
基库删除了多态比较。您可以这样使用运算符:
open Base
let () =
let tup = ("hello", "world") in
let h, w = tup in
if String.(h = w) then print_endline "equal"
else print_endline "not equal"编辑:如果解释没有说明清楚,则基础库不会影响OCaml处理元组的方式。
https://stackoverflow.com/questions/67843828
复制相似问题