英文:
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 -> 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 '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.
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 -> 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论