4

I'm trying to use some library code written in scala from a java program. I have a function that returns an Array (a scala Array) and I thought it would be possible to do

Tree[] = ScalaObject.myScalaFunction()

But the I get this error :

[error] found   : scala.runtime.BoxedArray
[error] required: org.grammaticalframework.Trees.Absyn.Tree[]

What is the correct way to use a scala array in java ?

4
  • 1
    What's the sig of myScalaFunction() look like? Commented Jun 3, 2010 at 13:13
  • 2
    Do you use scala 2.7? In scala2.8, a scala Array is equals to a java Array. Commented Jun 3, 2010 at 13:22
  • Assuming that myScalaFunction() is really returning an array of Trees, have you just tried casting the returned value appropriately? Commented Jun 3, 2010 at 13:23
  • The function is actually a call to toArray from a scala.List (toArray[B >: A] : Array[B]) I'm using scala 2.7.7 Casting doesn't work. Commented Jun 3, 2010 at 13:33

1 Answer 1

10

With 2.7, you should be able to

Tree[] t = (Tree)ScalaObject.myScalaFunction().unbox(Tree.class);

in Java.

With 2.8, it will work as you hoped it would.

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

Comments

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.