이 간단한 스칼라 코드와 상호 운용성을 시도하고 있지만 몇 가지 문제가 있습니다. Clojure의에서클로저/스칼라 interop?
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)
}
}
, 난이
(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가 스칼라 코드를 사용할 수 있도록 어떻게 수정할 수 있습니까?
고마워요! Decompiling 트릭을 했어 - 자바 코드가 완벽했다, 그래서 결국 내 증분 빌드 도구와 함께 이상한 버그 였어 - 다시 시작하고 예상대로 일했다! btw, 나는 http://java.decompiler.free.fr/, 훌륭한 도구를 사용했습니다. – josh
@josh :'scalac -print'를 시도해 볼 수도 있습니다. "desugared"버전은 여전히 스칼라 문법이지만 Java와 동질성입니다. 즉 모든 고급 스칼라 기능이 제거 된 것입니다. 또한'javap '처럼 작동하는'scalap'이 있습니다. 즉'.class' 파일을 디 컴파일합니다. –
@ josh : 듣기 좋게 잘 작동합니다! –