6

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?

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Decompiling did the trick - java code was perfect, so in the end it was some weird bug with my incremental build tool - restarted and it worked as expected! btw, i used java.decompiler.free.fr, great tool.
@josh: You can also try scalac -print which will print out a "desugared" version which is still Scala syntax, but isomorphic to Java, i.e. with all advanced Scala features removed. Also, there is scalap which works like javap, i.e. decompiles .class files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.