gpt4 book ai didi

java - Clojure deftype : how to constrain field types?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:55:05 25 4
gpt4 key购买 nike

我正在尝试编写一个可以从 Java 使用的 Clojure 库,而用户不知道它是用 Clojure 编写的。为此,我需要我的字段具有正确的类型:

我喜欢我能做到这一点:

(deftype Point [^double x ^double y])

这会为 x/y 生成一个具有适当类型的类。然而,这似乎只适用于原语,不适用于类:

(deftype Foo [^String bar])

生成一个:

public final Object bar;

我期望的地方:

public final String bar;

有没有办法限制字段类型? deftype/defrecord 之外还有其他选项吗?

最佳答案

来自 http://clojure.org/datatypesdeftypedefrecord 上:

fields can have type hints, and can be primitive

note that currently a type hint of a non-primitive type will not be used to constrain the field type nor the constructor arg, but will be used to optimize its use in the class method

constraining the field type and constructor arg is planned

(我的重点)

关于java - Clojure deftype : how to constrain field types?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12122831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com