如何在 OCAML 类中声明记录

发布于 2024-07-14 21:12:19 字数 560 浏览 1 评论 0原文

我想在类中声明一条记录,如下所示:

class player (x, y)=
     object(self)
     type gun = {x:int; y:int; active:bool}
     val guns = Array.create 5 {x=0; y=0; active=false}
....

但编译器声称这一行是语法错误: typegun = {x:in ....

当在类外部声明时,

type :  gun = {x:int; y:int; active:bool}
class player (x, y)=
     object(self)
     val guns = Array.create 5 {x=0; y=0; active=false}
....

错误是:unbound valuegun。

那么有人知道如何用另一种方式达到相同的功能吗? 谢谢你!

********** 解决了***

奇怪的是,现在当类型在外部声明时它就可以工作了,谢谢

I wanna declare a record inside a class as follows:

class player (x, y)=
     object(self)
     type gun = {x:int; y:int; active:bool}
     val guns = Array.create 5 {x=0; y=0; active=false}
....

but the compiler claim that this line is syntax error : type gun = {x:in ....

when declared outside the class like this

type :  gun = {x:int; y:int; active:bool}
class player (x, y)=
     object(self)
     val guns = Array.create 5 {x=0; y=0; active=false}
....

the error is : unbound value gun.

so anyone know how to reach the same functionality with another way?
thank you!

********* solved***

Bizare now it's working when the type is declared outside, thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-07-21 21:12:19

为什么不在类定义之外定义类型枪?

Why don't you define the type gun outside of the class definition?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文