3

I have java product with rich set of api. I want to write scala api as a wrapper over those java api. Are there any guidelines for the same. Please share your experience

Avinash
  • 12,851
  • 32
  • 116
  • 186
  • You might want to look at Pimp/enrich my library approach: http://stackoverflow.com/questions/5410846/how-do-i-apply-the-enrich-my-library-pattern-to-scala-collections – Adam Gent Feb 02 '14 at 16:53
  • 2
    You haven't accepted answers to your previously asked questions.... – Jatin Feb 02 '14 at 17:23
  • This question is way too vague. There are as many reasons for writing a Scala wrapper for a Java library as there are for writing a library in Scala. What are you trying to accomplish? – Travis Brown Feb 02 '14 at 17:52
  • Perhaps if you offered some API samples to give an idea of the kind of thing you want to wrap? Feel free to change names to protect the innocent... – Kevin Wright Feb 02 '14 at 18:00
  • Considering Scala runs in the JVM, why is an extra API needed? – Stewart Feb 02 '14 at 20:28

1 Answers1

3

This is a very vague question as others have noted, but I suppose there are some broad suggestions:

  • Use JavaConverters to translate Java collections to Scala collections.
  • Use Scala annotations to represent properties or characteristics represented by other means in Java. Examples of these include @deprecated, @throws, and @BeanProperty. @BeanProperty is especially useful if you want to use a library that specifically demands JavaBeans (i.e conforms to the specification).
  • If the Java code uses Spring, maybe look into Spring Scala if necessary. Or use more constructor-args. Or asInstanceOf to cast any beans you manually fetch from the context.
  • Build files. Perhaps you want to replace any Java-specific build mechanism with SBT. Or transform your Java-focused Gradle build file to be more Scala-focused. That kind of thing.

That's all I can think of. Hopefully others will point out other considerations I've missed.

sourcedelica
  • 23,940
  • 7
  • 66
  • 74
Vidya
  • 29,932
  • 7
  • 42
  • 70