我试图将这个简单的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代码?
scalac -print
命令,它会打印出一个“解糖”版本,该版本仍然是Scala语法,但与Java同构,即所有高级Scala功能都被移除。此外,还有scalap
命令,类似于javap
,即反编译.class
文件。 - Jörg W Mittag