Functional programming meets the client side: Scala.js

Functional programming meets the client side: Scala.js

Although I have been developing apps on the backend side for many years, I’ve always been strongly attracted to front-end technologies, especially those which improve user experiences.

In the web environment, there are few options when choosing a language on the client side. No matter how many frameworks or libraries outcrop, possibilities are reduced to JavaScript, which is a scripting language designed for programming in the small.

I had the pleasure of attending the live coding session by Sébastien Doeraene at 2014 Scala eXchange and was delighted by the virtues of Scala.js. For those who are unaware, it’s a compiler from Scala to JavaScript. That seems easier said than done. But it actually opens a window of opportunity since, like regular Scala, we get all the powerful language features that we know and love including type inference, classes, traits and objects, pattern matching, the collections library, implicits, etc. And, in the same way that Scala is fully interoperable with Java, Scala.js is fully interoperable with JavaScript. Scala.js can interoperate with JavaScript either in a statically or dynamically-typed way.

Several weeks ago I read that, after two years of development, Scala.js was no longer experimental. The semantics of the language, the standard library, and the sbt builds will remain backward source compatible. Therefore, it became production-ready! I knew it was the right time to carry out my first attempt at Scala.js. I had this idea of making a self-referent project that teaches the required steps to make your own site. You can take a look at this simple project in this repository, and the result here:

The little bit of experience I have with Scala.js has made me realize the potential value it offers. I’ll mention some of the most highly-rated aspects by the community, to which I concur.

  • Share code between client and server
  • Strong typing, including for JavaScript libraries
  • Tooling: IDE support, sbt integration, dependency, unit testing, stack traces and source maps, cross-compiling projects
  • Portability: bring Scala to environments powered by JavaScript, most importantly the browsers, but also others, such as the brand new React Native
  • Integration with JavaScript
  • Fast compilation cycle
  • The Scala Standard library
  • Great community, welcoming to new users and helpful
  • Very good documentation

The first one is of special importance to me, in particular. It’s the first time we can address the development of scalable and reactive web projects using the same language, end-to-end, from server to client-side. When sharing models, for example, you can forget the tedious conversion process with free serialization-deserialization between sides. Another “epic-win” is the impressive interoperability with JavaScript code. For example, you can use jQuery and HTML5 from your Scala.js code, either in a typed or untyped way. Or create Scala.js objects and call their methods from JavaScript.

In addition to all this noteworthy goodness, the future is looking promising. The community is currently working on some cool features including Scala.js REPL in the browser, some UI DSL, Scala.js and Play, Akka in Scala.js. They’re also striving to reduce the size of the Scala standard library which is currently 16 MB of JavaScript code after compression by Google Closure’s simple optimizations.

In conclusion, I would like to emphasize the possibilities. Scala might be a nice language, but you’ve never been able to use it to make anything cool that a non-backend-systems person would understand, until now.

As always, thanks for reading! If you have any questions or suggestions, don’t hesitate to start the conversation on Facebook, Twitter, or in the comments below.

blog comments powered by Disqus

Ensure the success of your project

47 Degrees can work with you to help manage the risks of technology evolution, develop a team of top-tier engaged developers, improve productivity, lower maintenance cost, increase hardware utilization, and improve product quality; all while using the best technologies.