Clojure/Scala 互操作?

发布于 2024-10-08 00:38:21 字数 1365 浏览 1 评论 0原文

我正在尝试与这个简单的 scala 代码进行互操作,但遇到了一些麻烦。

package indicators

class DoubleRingBuffer(val capacity:Int=1000) {
  var elements = new Array[Double](capacity);
  private var head=capacity-1
  private var max=0

  def size ():Int = {
    return max+1
  }

  def add(obj:Double):Double = {
    head-=1
    if (head<0) head=capacity-1
    return set(max+1,obj)
  }

  def set(i:Int,obj:Double):Double = {
    System.out.println("HI")
    if (i>=capacity || i<0)
      throw new IndexOutOfBoundsException(i+" out of bounds")
    if (i>=max) max=i
    var index = (head+i)%capacity
    var prev = elements(index)
    elements(index)=obj
    return prev
  }

  def get(i:Int=0):Double = {
    System.out.println("size is "+size())
    if (i>=size() || i<0)
      throw new IndexOutOfBoundsException(i+" out of bounds")
    var index = (head+i)%capacity
    return elements(index)
  }    
}

在 clojure 中,我这样做此外

(import 'indicators.DoubleRingBuffer)
(def b (DoubleRingBuffer. 100))
(pr (.size b)) ;;ERROR: No matching field found: size for class indicators.DoubleRingBuffer
(pr (.get b 33)) ;;returns 0: should throw an index out of bounds error!
(pr (.get b 100)) ;;throws index out of bounds error, as it should

,我没有在控制台上得到任何输出!使用 scala 测试此代码可以按预期工作。这是怎么回事,我该如何修复它以便 clojure 可以使用 scala 代码?

I am attempting to interop to this simple scala code, but am having some troubles.

package indicators

class DoubleRingBuffer(val capacity:Int=1000) {
  var elements = new Array[Double](capacity);
  private var head=capacity-1
  private var max=0

  def size ():Int = {
    return max+1
  }

  def add(obj:Double):Double = {
    head-=1
    if (head<0) head=capacity-1
    return set(max+1,obj)
  }

  def set(i:Int,obj:Double):Double = {
    System.out.println("HI")
    if (i>=capacity || i<0)
      throw new IndexOutOfBoundsException(i+" out of bounds")
    if (i>=max) max=i
    var index = (head+i)%capacity
    var prev = elements(index)
    elements(index)=obj
    return prev
  }

  def get(i:Int=0):Double = {
    System.out.println("size is "+size())
    if (i>=size() || i<0)
      throw new IndexOutOfBoundsException(i+" out of bounds")
    var index = (head+i)%capacity
    return elements(index)
  }    
}

In clojure, i do this

(import 'indicators.DoubleRingBuffer)
(def b (DoubleRingBuffer. 100))
(pr (.size b)) ;;ERROR: No matching field found: size for class indicators.DoubleRingBuffer
(pr (.get b 33)) ;;returns 0: should throw an index out of bounds error!
(pr (.get b 100)) ;;throws index out of bounds error, as it should

In addition, i do not get any output to the console! Testing this code using scala works as expected. Whats going on here and how can i fix it so that clojure can use the scala code?

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

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

发布评论

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

评论(1

氛圍 2024-10-15 00:38:21

在 REPL 中尝试这些:

(class b) 可能会告诉您它是 indicators.DoubleRingBuffer

(vec (.getDeclaredMethods (class b))) 将为您提供类中声明的所有方法的向量,就好像它是 Java 类一样,以便您可以看到它们的签名。

现在,使用这些方法名称和参数来调用签名中所示的方法。

我有一种感觉,问题出在 Scala 处理方法参数默认值的过程中。

编辑:正如OP在评论中所描述的那样,事实并非如此。

如果这不起作用,您可以尝试将 Scala 字节码反编译为 Java,以了解 DoubleRingBuffer 类的外观。

Try these in REPL:

(class b) will probably tell you it's indicators.DoubleRingBuffer.

(vec (.getDeclaredMethods (class b))) will give you a vector of all methods declared in your class as if it was a Java class, so you can see their signatures.

Now, call your methods as seen in the signatures, with these method names and parameters.

I have a feeling the problem is in Scala's dealing with default value for method parameter.

EDIT: As OP described in a comment, it isn't.

If that doesn't work you can try to decompile your Scala bytecode to Java to find out how does DoubleRingBuffer class look like.

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