This page is no longer maintained — Please continue to the home page at www.scala-lang.org

What is the recommended way to implement Java interface with Java collections in Scala ?

2 replies
Andrey Somov
Joined: 2011-03-05,
User offline. Last seen 42 years 45 weeks ago.
Hi all,
the task:

Implement a Java interface in Scala. The implementation shall be used both in Java and in Scala.

It looks like an easy task. And it was, when the interface contained only String, Integer and Boolean types.
Once the interface has got Java collections it became a disaster.
The problem is not that it is difficult to implement, the problem is that the implementation is ugly. Java collections penetrate to the Scala implementation and I constanly have to translate collections forth and back.

 What is the recommended way to implement Java interface with Java collections in Scala ?

I think to accept the following way:
1) Deliver 2 independent identical interfaces - a Java interface and a Scala trait.
2) Implement only Scala trait (using only Scala :)
3) Java interface is implemented as an adaptor around the Scala implementation from 2). The main purpose of this adaptor is to translate the collections.

Is there a better way ?

-
Andrey


Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: What is the recommended way to implement Java interface wit
Split interfaces is definitely doable, it's also worth remembering that a Scala trait is implemented as a Java interface behind the scenes, and is readily usable as such.
In this scenario, I'll follow language conventions for property naming and let things just fall into place naturally.

trait ScalaInterface {  def entries: List[String] }


import collection.JavaConverters._
trait JavaInterface {  self: ScalaInterface =>   def getEntries = entries.asJava}


class MyClass extends ScalaInterface with JavaInterface {  val entries: List[String] = List(1,2,3) }

You can also avoid ScalaInterface entirely:
import collection.JavaConverters._
trait JavaInterface {   self: MyClass =>  def getEntries = entries.asJava}


class MyClass extends JavaInterface {   val entries: List[String] = List(1,2,3)}



On 21 December 2011 15:16, Andrey Somov <trophybase@googlemail.com> wrote:
Hi all,
the task:

Implement a Java interface in Scala. The implementation shall be used both in Java and in Scala.

It looks like an easy task. And it was, when the interface contained only String, Integer and Boolean types.
Once the interface has got Java collections it became a disaster.
The problem is not that it is difficult to implement, the problem is that the implementation is ugly. Java collections penetrate to the Scala implementation and I constanly have to translate collections forth and back.

 What is the recommended way to implement Java interface with Java collections in Scala ?

I think to accept the following way:
1) Deliver 2 independent identical interfaces - a Java interface and a Scala trait.
2) Implement only Scala trait (using only Scala :)
3) Java interface is implemented as an adaptor around the Scala implementation from 2). The main purpose of this adaptor is to translate the collections.

Is there a better way ?

-
Andrey





--
Kevin Wright
mail: kevin.wright@scalatechnology.com
gtalk / msn : kev.lee.wright@gmail.com quora: http://www.quora.com/Kevin-Wrightgoogle+: http://gplus.to/thecoda
kev.lee.wright@gmail.com twitter: @thecoda
vibe / skype: kev.lee.wrightsteam: kev_lee_wright
"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
Andrey Somov
Joined: 2011-03-05,
User offline. Last seen 42 years 45 weeks ago.
Re: What is the recommended way to implement Java interface wit
it looks like less code duplication then my approach with the adaptor.
I will give it a try to see how the implementation will look like.

Thank you very much,
-
Andrey

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland