OCaml literal negative number?

 ̄綄美尐妖づ 提交于 2019-12-05 12:00:58

You need to enclose it in order to avoid parsing amiguity. "test_threeways -10" could also mean: substract 10 from test_threeways.

And there is no function application involved. Just redefine the unary minus, to see the difference:

#let (~-) = (+) 2 ;; (* See documentation of pervarsives *)
val ( ~- ) : int -> int = <fun>
# let t = -2 ;; 
val t : int = -2 (* no function application, constant negative number *)
# -t ;;
- : int = 0   (* function application *)

You can use ~- and ~-. directly (as hinted in the other answer), they are both explicitly prefix operators so parsing them is not ambiguous. However I prefer using parentheses.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!