【问题标题】:Ocaml: Error - this expression has type x but is used with type xOcaml:错误 - 此表达式具有类型 x,但与类型 x 一起使用
【发布时间】:2015-08-30 18:28:47
【问题描述】:

这是我的错误:

Error: This expression has type nfa but is here used with type nfa

可能会发生什么导致这种情况?我正在使用 emacs tuareg,并一一加载评估文件。有时会发生这种情况,有时则不会。

【问题讨论】:

  • 你能贴出产生这个错误的代码吗?

标签: ocaml tuareg


【解决方案1】:

ocaml tutorial 对此有很好的描述。发生的事情是您使用新定义遮蔽了类型定义:

type nfa = int
let f (x: nfa) = x

type nfa = int
let g (x: nfa) = x

重新启动顶层将清除旧定义。

【讨论】:

    【解决方案2】:

    更新:

    自 OCaml 4.01.0(2013 年 9 月发布)以来,一般问题是相同的,但错误消息在类型定义中添加了一个数字,以表明类型在内部是不同的。

    顶层old OCaml FAQ的完整示例:

    type counter = Counter of int;; (* define a type *)
    type counter = Counter of int
    # let x = Counter 1;;           (* use the new type *)
    val x : counter = Counter 1
    type counter = Counter of int;; (* redefine the type, use it *)
    type counter = Counter of int
    # let incr_counter c = match c with Counter x -> Counter (x + 1);;
    val incr_counter : counter -> counter = <fun>
    # incr_counter x;;              (* now mix old and new defs *)
    Error: This expression has type counter/1029
           but an expression was expected of type counter/1032
    # 
    

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多