- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Help with Scala/Java Integration
Tue, 2012-01-24, 03:54
Hello,
When using (Java) swing and Scala together I found this difference and
don't know how to solve it. Can anyone please help?
Why is the Scala result different and how the Scala code should be
modified to give the same result as in Java?
TreePath class has 2 constructors: TreePath(Object) and
TreePath(Object[])
It seems that the former constructor is being used in Scala (line 9),
but the val "path" is an array. (Doesn't help coding
... new TreePath(path.asInstanceOf[Array[TreeNode]])
nor
val path : Array[TreeNode] = author.getPath()
)
Thank in advance,
Marcos
Tue, 2012-01-24, 12:51
#2
Re: Help with Scala/Java Integration
Hi Johannes,
using your suggestion
`new TreePath(path.asInstanceOf[Array[AnyRef]])`
worked fine. I've _many_ things to learn in Scala.... Thank you very
much!
Marcos
On Jan 24, 5:08 am, Johannes Rudolph
wrote:
> Hi Marcos,
>
> in Scala `Array[TreeNode]` is no subtype of `Array[AnyRef]` which is
> the equivalent of `Object[]` in Java. That means that the
> `TreeNode(Object[])` constructor can't be selected. To make it
> eligible you have create an `Array[AnyRef]` directly (or cast to it).
>
> Using `new TreePath(path.asInstanceOf[Array[AnyRef]])` should work.
>
> The underlying cause is that in Java arrays are 'covariant. In this
> case that means that in Java `Object[]` is a superclass of
> `TreeNode[]` and you can always pass a `TreeNode[]` value when an
> `Object[]` value is expected.
>
> Because Array's covariance is potentially unsafe because arrays are
> mutable, in Scala arrays are invariant. That means that
> `Array[TreeNode]` is no subtype of `Array[AnyRef]`.
>
> HTH
> Johannes
>
Hi Marcos,
in Scala `Array[TreeNode]` is no subtype of `Array[AnyRef]` which is
the equivalent of `Object[]` in Java. That means that the
`TreeNode(Object[])` constructor can't be selected. To make it
eligible you have create an `Array[AnyRef]` directly (or cast to it).
Using `new TreePath(path.asInstanceOf[Array[AnyRef]])` should work.
The underlying cause is that in Java arrays are 'covariant. In this
case that means that in Java `Object[]` is a superclass of
`TreeNode[]` and you can always pass a `TreeNode[]` value when an
`Object[]` value is expected.
Because Array's covariance is potentially unsafe because arrays are
mutable, in Scala arrays are invariant. That means that
`Array[TreeNode]` is no subtype of `Array[AnyRef]`.
HTH
Johannes
On Tue, Jan 24, 2012 at 3:54 AM, Marcos Ackel wrote:
> Hello,
>
> When using (Java) swing and Scala together I found this difference and
> don't know how to solve it. Can anyone please help?
>
> Why is the Scala result different and how the Scala code should be
> modified to give the same result as in Java?
>
> TreePath class has 2 constructors: TreePath(Object) and
> TreePath(Object[])
>
> It seems that the former constructor is being used in Scala (line 9),
> but the val "path" is an array. (Doesn't help coding
> ... new TreePath(path.asInstanceOf[Array[TreeNode]])
> nor
> val path : Array[TreeNode] = author.getPath()
> )
>
> Thank in advance,
>
> Marcos
>
> ---- in Java -----------------------
> DefaultMutableTreeNode root = new DefaultMutableTreeNode();
> root.setUserObject("root");
> DefaultMutableTreeNode author = new DefaultMutableTreeNode();
> author.setUserObject("Twain");
> root.add(author);
>
> DefaultMutableTreeNode book = new DefaultMutableTreeNode();
> book.setUserObject("Huck");
> TreeNode[] path = author.getPath();
> TreePath tp = new TreePath(path);
> TreePath tp2 = tp.pathByAddingChild(book);
> System.out.println(tp2.getPathCount());
> for (int i = 0; i < tp2.getPathCount(); i++)
> System.out.println("p" + i + " " + tp2.getPathComponent(i));
> -----------
> Result:
>
> 3
> p0 root
> p1 Twain
> p2 Huck
> ----------------------- In Scala
> --------------------------------------------
> 1 val root = new DefaultMutableTreeNode()
> 2 root.setUserObject("root")
> 3 val author = new DefaultMutableTreeNode()
> 4 author.setUserObject("author")
> 5 root.add(author)
>
> 6 val book = new DefaultMutableTreeNode()
> 7 book.setUserObject("Huck")
> 8 val path = author.getPath()
> 9 val tp = new TreePath(path)
> 10 val tp2 : TreePath = tp.pathByAddingChild(book)
> 11 println(tp2.getPathCount())
> 12 for ( i <- 0 until tp2.getPathCount())
> 13 println("p" + i + " " + tp2.getPathComponent(i))
> ---------------
> Result:
>
> 2
> p0 [Ljavax.swing.tree.TreeNode;@6f7ce9
> p1 Huck
>
> ---------------------------------------