将构造函数添加到 deftype 创建的类中
为了与 Java 实现互操作性,我需要一个具有执行初始化的无效构造函数的类。 此类的对象需要具有类似于可变java字段的东西(即该对象代表游戏的后端,并且需要保持游戏状态)。
deftype 可以做我想做的一切,除了提供一个空的构造函数(因为我正在创建一个带有字段的类)。
我不需要这些字段是公开可读的,所以我可以想到 4 个解决方案:
使用 gen-class;如果可以避免的话我不想这样做。
以某种方式在 deftype 知识之外对私有成员变量进行编码;我被告知这是不可能的。
编写一个修改后的 deftype,它也会创建一个空构造函数;坦率地说,我对此不太了解 clojure。
采用 deftype 创建的类并以某种方式向其中添加一个新的构造函数。
最后,我需要一个 Java 类,因为我将把它交给 Java 代码,该代码将从该类创建一个新对象。
除了使用 gen-class 之外,我建议的任何解决方案(或任何我没有想到的)是否可行?
For the purposes of interoperability with Java, I need a class that has a nullary constructor that performs initialization.
Objects of this class need to have something resembling mutable java fields (namely, the object represents the backend of a game, and needs to keep game state).
deftype does everything I want to do except provide a nullary constructor (since I'm creating a class with fields).
I don't need the fields to be publicly readable, so I can think of 4 solutions:
Use gen-class; I don't want to do this if I can avoid it.
Somehow encoding private member variables outside of the knowledge of deftype; I've been told this can't be done.
Writing a modified deftype that also creates a nullary constructor; frankly I don't know clojure well enough for this.
Taking the class created by deftype and somehow adding a new constructor to it.
At the end of this, I need to have a Java class, since I will be handing it off to Java code that will be making a new object from the class.
Are any of the solutions I suggested (or any that I haven't thought of) other than using gen-class viable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 Java 互操作需求既具体又不可动摇,那么在适当的情况下编写少量 Java 绝对没有什么可耻的。您可以编写一个带有单个静态工厂方法的 Java 类,该方法返回 deftype 类的实例,并执行您需要的任何初始化/设置。
或者,您可以在 Clojure 中编写一个空工厂函数,然后整天直接从 Java 调用它。
无论如何,
deftype
和defrecord
都不是(或永远不会是)全功能的互操作工具。gen-class
当然是最接近的,这就是推荐它的原因。There's absolutely no shame in, where appropriate, writing a dash of Java if your Java interop requirements are simultaneously specific and unshakable. You could write a Java class with a single static factory method that returns an instance of the
deftype
class and that does whatever initialization/setup you need.Alternatively, you can write a nullary factory function in Clojure, and call that directly from Java all day long.
In any case, neither
deftype
nordefrecord
are intended to be (or will they ever be) fully-featured interop facilities.gen-class
certainly comes the closest, which is why it's been recommended.我建议只用 Java 编写对象——对于具有可变字段的类似 Java 的对象,它可能会更优雅、更容易理解和更实用。
我在项目中混合 Java 和 Clojure 代码通常取得了相当好的结果。这似乎是适合这样做的情况之一。互操作性非常好,几乎没有任何额外的复杂性。
顺便说一句 - 我假设您需要一个无效构造函数来满足某些持久性库或类似库的要求?否则这似乎是一个奇怪的要求。如果是这种情况,那么您可能会发现重新考虑您的持久性策略是有意义的……这样的任意限制对我来说总是有点代码味道。
I'd suggest just writing the object in Java - for Java-like objects with mutable fields it will probably be more elegant, understandable and practical.
I've generally had pretty good results mixing Java and Clojure code in projects. This seems like one of those cases where this might be appropriate. The interoperability is so good that you barely have any extra complexity.
BTW - I'm assuming that you need a nullary constructor to meet the requirements of some persistence library or something similar? It seems like an odd requirement otherwise. If this is the case then you may find it makes sense to rethink your persistence strategy..... arbitrary restrictions like this always seem like a bit of a code smell to me.