Appendix G. Polyglot development

As you’ve seen throughout the book, DSLs are not limited by a single programming language. You can use the language that best fits your requirements. Even though it seems that this means that your application will become a nest of chaos between indiscriminate islands of language cacophony, that doesn’t have to be the case. But how will you know when your project has entered such an unfortunate state? Simple! You’ll feel like the confused programmer in figure G.1.

Figure G.1. Don’t let this happen to you!

In this appendix, you’ll see how to bootstrap yourself into a disciplined polyglot development environment. I assume that you’ll be developing DSL-based applications on the JVM. In such developments, Java can play the role of the base host language where your main application is developed using Java, and you’re developing DSLs in some other language to make your published APIs expressive to your clients. Just a friendly reminder—this appendix is only for entry-level users planning to start developing DSLs using a mixed-language paradigm. If you’re already experienced in such development patterns, feel free to skip this appendix.

I’m going to discuss two examples of how to bootstrap your development environment in case you decide to develop your DSL in a non-Java language on the JVM and integrate it with your Java-based application. The first example is based on Groovy, which is a dynamic lan guage. You’ll get a feel for how modern IDEs give you a seamless integration of a mixed Java-Groovy project. The second example is based on Scala, which is a statically typed language. In that example, you’ll see how to set up your environment for a mixed Java-Scala project.

G.1. What features should you look for in an IDE?

When you’re working on a polyglot project in languages on the JVM, you’ll want to look for the following features in your IDE:

  • Support for a mixed Java-X project where X is Scala, Groovy, Ruby, or Clojure, and any combination of Java/X project dependencies.
  • A rich syntactic editor integrated with the IDE that provides some amount of assistance to the developer. The richness of the editor means support for syntax highlighting, inferred types, documentation on hover, code completion, and the like.
  • A unified explorer view of all project artifacts, including types, packages, and views of the combined project.
  • Debugging support integrated for the languages.

Additionally, there will be other features that vary across languages. Generally, statically typed languages offer better IDE support than dynamic ones because there’s more meta-information available to them. But things are improving every day. Many popular IDEs are becoming more and more user-friendly, offering a better experience to the developers.

G.2. Bootstrapping a Java-Groovy development environment

If you’ve worked on any nontrivial Java project, you must’ve had the experience of using a modern IDE like Eclipse (http://eclipse.org) or NetBeans (http://netbeans.org). When you use multiple languages for developing DSLs, I’m sure you’ll also be looking for similar user friendliness in interaction and building project artifacts from your IDE. This is something that’s evolving quickly—keep an eye on your favorite platform for all the recent updates.

Groovy is a language that has good integration with Java. As I mention in chapter 3, Groovy shares the object model with Java; any IDE that works well for Java-based projects should also work equally well for Groovy-based ones. But there’s a catch. Groovy is a dynamic language where you can optionally specify types. In many cases, the compile-time type information isn’t present with the IDE. Code completion or other rich features that the editor offers might not be that refined in Groovy. Even so, things are improving and we’re seeing lots of plugins being developed that perform these smart acts in the editor, even for dynamic languages.

To bootstrap your Groovy-based DSL development environment in a Java project, take the advice given in table G.1.

Table G.1. Steps for setting up a Groovy based DSL development environment

Do

Use it for

Download the Java Development Kit (version 5 or 6). Regular Java development, as well as the runtime for Groovy-based development.
Download the NetBeans IDE (latest version); for exact version compatibility information, check out the documentation at http://netbeans.org. The IDE that you’ll use for hosting Java and Groovy projects.
Create a regular Java application from the NetBeans menu. This application will be the one that can host Java and Groovy source files.
Name the project and start creating Java and Groovy source files. The IDE that will provide an integrated view of the Java and Groovy project artifacts. The Groovy DSL scripts will appear alongside Java source files, based on the package structure that you design. Without any additional plugin, you’ll be able to build your project from within Netbeans. Your DSL scripts can be happily invoked from within Java classes, using one of the methods that we discuss in chapters 2, 3, and 4.

G.3. Bootstrapping a Java-Scala development environment

Scala, as we’ve discussed earlier, is statically typed with a powerful type system. IDE support for Scala is evolving—we already have quite good support for Scala editing through Eclipse, IntelliJ Idea, and NetBeans.

Installing Scala support in Eclipse is as simple as pulling in the latest version of the plugin. Here’s the complete rundown of what you need to do to bootstrap your Eclipse environment to support a mixed Java/Scala development.

Software requirements:

  • Java Development Toolkit 1.6
  • Eclipse Classic 3.5.2 (Check the exact version at http://eclipse.org)

After you’ve installed Eclipse, you need to install the plugin for Scala development. On the home page for Scala IDE (http://www.scala-ide.org/), there’s a friendly video that takes you through the steps in detail.

Eclipse plugin for Scala is improving daily. Now with the plugin installed, you get a rich set of features for developing a mixed Scala/Java project:

  • Support for Scala/Java project development
  • Rich editor support with code completion, type inferencing, and so on
  • Incremental compilation
  • Debugger support
  • A lot more for all the artifacts of Scala and Java

G.4. Popular IDEs for polyglot development

Table G.2 provides a list of some of the commonly used IDEs for developing applications that use multiple languages. With each of the IDEs, I’ve listed the corresponding required plugins and the common languages that they support.

Table G.2. Polyglot IDEs

IDE

Plugin support

Eclipse (http://eclipse.org) Plugin support for:
NetBeans (http://netbeans.org) Plugin support for:
Emacs (http://www.gnu.org/software/emacs/) Among all the JVM languages, Emacs is possibly the number one choice for Clojure editing. One point of warning though: unless you’re familiar with Emacs, it takes some time to get into the mode. If you want to try out the Emacs-Clojure combination, go to http://www.assembla.com/wiki/show/clojure/Getting_Started_with_Emacs
IntelliJ IDEA (http://www.jetbrains.com/idea/) Plugin support for:

IDEs are improving every day and are getting richer with new features. Please check the URL for your preferred IDE before you decide to jump on it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.156.250