OCaml: This expression has type int but an expression was expected of type MyFoo.ty (except MyFoo.ty is int)

huangapple go评论64阅读模式
英文:

OCaml: This expression has type int but an expression was expected of type MyFoo.ty (except MyFoo.ty is int)

问题

以下是您的代码的翻译部分:

这是我的代码

foo.ml

module type ITy = sig
   type ty
end

module type IFoo = sig
   type ty
   val doo: ty -> ty
end

module Make (Ty: ITy): IFoo = struct
   type ty = Ty.ty
   let doo (x:ty) = x
end

main.ml

module MyFoo = Foo.Make(struct
   type ty = int
end)

let () = assert(MyFoo.doo 3 = 3)

要编译我使用

$ corebuild main.native

这将给我

+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -bin-annot -short-paths -thread -package core -ppx 'ppx-jane -as-ppx' -o main.cmo main.ml
File "main.ml", line 5, characters 26-27:
5 | let () = assert(MyFoo.doo 3 = 3)
                              ^
Error: This expression has type int but an expression was expected of type
         MyFoo.ty
Command exited with code 2.
Hint: Recursive traversal of subdirectories was not enabled for this build,
  as the working directory does not look like an ocamlbuild project (no
  '_tags' or 'myocamlbuild.ml' file). If you have modules in subdirectories,
  you should add the option "-r" or create an empty '_tags' file.

  To enable recursive traversal for some subdirectories only, you can use the
  following '_tags' file:

      true: -traverse
      <dir1> or <dir2>: traverse

Compilation unsuccessful after building 4 targets (3 cached) in 00:00:01.

我预期我的代码能够成功编译因为在main.ml中调用`Foo.Make`函数以创建`MyFoo`我给它一个输入结构体`type ty = int`我可能做错了些什么但是一直没有找到明确讨论这个问题的文档也许我在某个地方使用了错误的运算符任何帮助将不胜感激
英文:

Here's my code:

foo.ml:

module type ITy = sig
type ty
end
module type IFoo = sig
type ty
val doo: ty -&gt; ty
end
module Make (Ty: ITy): IFoo = struct
type ty = Ty.ty
let doo (x:ty) = x
end

main.ml:

module MyFoo = Foo.Make(struct
type ty = int
end)
let () = assert(MyFoo.doo 3 = 3)

To compile, I use:

$ corebuild main.native

which gives me:

+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -bin-annot -short-paths -thread -package core -ppx &#39;ppx-jane -as-ppx&#39; -o main.cmo main.ml
File &quot;main.ml&quot;, line 5, characters 26-27:
5 | let () = assert(MyFoo.doo 3 = 3)
^
Error: This expression has type int but an expression was expected of type
MyFoo.ty
Command exited with code 2.
Hint: Recursive traversal of subdirectories was not enabled for this build,
as the working directory does not look like an ocamlbuild project (no
&#39;_tags&#39; or &#39;myocamlbuild.ml&#39; file). If you have modules in subdirectories,
you should add the option &quot;-r&quot; or create an empty &#39;_tags&#39; file.
To enable recursive traversal for some subdirectories only, you can use the
following &#39;_tags&#39; file:
true: -traverse
&lt;dir1&gt; or &lt;dir2&gt;: traverse
Compilation unsuccessful after building 4 targets (3 cached) in 00:00:01.

I expected my code to compile successfully b.c. I'm an OCaml noob, and when I call the Foo.Make functor in main.ml to create MyFoo I give it an input struct of type ty = int. I'm probably doing something wrong, but have been unsuccessful at finding the documentation that talks about this specifically. Maybe I'm using the wrong operator somewhere. Any help would be appreciated.

答案1

得分: 0

这是您想要实现的吗?

模块类型 ITy = sig
   类型 ty
end

模块类型 IFoo = sig
   类型 ty
   val doo: ty -> ty
end

模块 Make (Ty: ITy): (IFoo with type ty := Ty.ty) = 
struct
   let doo x = x
end

模块 MyFoo = Make(struct
   type ty = int
end)

let () = assert(MyFoo.doo 3 = 3)
英文:

Is this what you are trying to accomplish?

module type ITy = sig
type ty
end
module type IFoo = sig
type ty
val doo: ty -&gt; ty
end
module Make (Ty: ITy): (IFoo with type ty := Ty.ty) = 
struct
let doo x = x
end
module MyFoo = Make(struct
type ty = int
end)
let () = assert(MyFoo.doo 3 = 3)

huangapple
  • 本文由 发表于 2020年1月4日 12:25:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定