- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
What is the recommended way to implement Java interface with Java collections in Scala ?
Wed, 2011-12-21, 16:16
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
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
Thu, 2011-12-22, 11:51
#2
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
I will give it a try to see how the implementation will look like.
Thank you very much,
-
Andrey
In this scenario, I'll follow language conventions for property naming and let things just fall into place naturally.
You can also avoid ScalaInterface entirely:
On 21 December 2011 15:16, Andrey Somov <trophybase@googlemail.com> wrote:
--
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