clojure / scala interop? [英] clojure/scala interop?

查看:90
本文介绍了clojure / scala interop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 软件包指示符

我尝试交互操作这个简单的scala代码,
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
} b
$ b def add(obj:Double):Double = {
head- = 1
if(head< 0)head = capacity-1
return set 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 = + i)%capacity
var prev = elements(index)
元素(索引)= obj
返回prev
}

def get(i: Int = 0):Double = {
System.out.println(size is+ size())
if(i> = size()|| i < new IndexOutOfBoundsException(i +out of bounds)
var index =(head + i)%capacity
返回元素(索引)
}
}

在clojure中,我这样做

 (import'indicators.DoubleRingBuffer)
(def b(DoubleRingBuffer。 100))
(pr(.size b));;错误:找不到匹配字段:类indicator.DoubleRingBuffer的大小
(pr(.get b 33));;返回0:should throw一个索引越界错误!
(pr(.get b 100));; throws index out of bounds错误,因为它应该


$ b b

此外,我没有得到任何输出到控制台!使用scala测试此代码可按预期工作。

解决方案

尝试这些在REPL:

/ p>

(类b)可能会告诉你它 indicators.DoubleRingBuffer



(vec(.getDeclaredMethods(class b)))会给你一个所有方法的向量在你的类中声明,就像它是一个Java类,所以你可以看到他们的签名。



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



我有一个感觉,问题是在Scala的处理方法参数的默认值。



编辑:作为注释中描述的OP,它不是。



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


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?

解决方案

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.

这篇关于clojure / scala interop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆