Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

May 10, 2008

Scala lift-off: Martin Odersky Keynote

I'm at the Scala lift-off today in San Francisco, an unconference organized by Dave Pollak, author of the lift web framework and frequent hanger-out at the Twitter office (or Twoffice if you will).

Martin Odersky, author of the Scala programming language, is kicking off the day. Martin teaches at EPFL in Switzerland, and has a background in functional languages. Before Scala, he's had a hand in several functional languages on the JVM, as well as rewriting the Java compiler for performance and correctness. The Scala effort started in 2002, but the community has really been growing since 2006 when a redesigned implementation of the language was released. This week has seen quite a bit of Scala buzz at JavaOne, including an endorsement of Scala as the premier alternative JVM language by Java author James Gosling.

Odersky opens with an argument for a scalable language: one that's suitable for small and large programs, from parsing to scripting, from extension scripts to heavy lifting libraries, one that embraces Domain-Specific Languages (DSLs) rather than a reliance on external languages.

In one sense, Odersky suggests, Scala is a scripting language. It's possible to simply type code into an interpreter and have it evaluated. There can be very little boilerplate code in Scala, and most types can be inferred.

In another sense, Scala is the "Java of the future." Scala boasts most every language feature Java currently has, and many that are still pending or under debate in the Java community, such as closures, traits, and pattern matching. Scala complies down to .class files, and Odersky jokes that he should know, given that he's written two versions of the shipping Sun Java compiler. Scala's performance and operational profile is thus the same as Java's, give or take special cases and the use of alternative libraries.

In still another sense, Scala is a "composition language", one with a new approach to modular systems. In Scala, components are classes or traits that can be composed via mixins. Abstraction is accomplished through abstract members, parameters, or self types; essentially, both functional and OOP metaphors for abstraction are available.

For all that, Scala is trying to avoid becoming a "kitchen-sink language". Scala is comparable in feature count to Java, but has far fewer features than C++ or C#. Scala removes the following from the Java feature set: static members, primitive types, break, continue, special treatment of interfaces, wildcards, raw types, and enums. Scala "concentrates on the glue", not on features. Leveraging abstraction, users can implement the features they need in the language, rather than the language itself supporting every possible use case. The same set of constructs are intended to support both small and large programs.

Scala's lightweight syntax comes from a number of features: semicolon inference, type inference, lightweight classes, extensible APIs, and closures as control abstractions. On average, Odersky predicts a 2x reduction in lines of code between a Java program and a Scala port.

Odersky suggests that "scalability demands extensibility." As an example, he notes all the different data types traditionally used for numerics in programming languages: int, long, float, double, complex, rational, interval, polynomial, etc. While a language could support every possible numeric data type, Scala's extensibility strategy encourages abstractions and additions to existing types and methods to provide new numeric data types seamlessly. For example, it's possible to define + and - methods, essential to dropping in YourFancyNumeric class without pain.

As another example of Scala's extensibility, Oderksy demonstrates adding new control structures. A using method executes a block of operations on a resource, then closes that resource. It's a simple pattern with a short implementation, but the terse code is expressive and requires intimate understanding of Scala's type system to understand what's going on.

Scala makes its scalability claims on the tight integration of functional and object-oriented programming. One of the important concepts here is that functions themselves are objects in Scala. Additionally, Scala offers Erlang-style Actors for high-concurrency programs, contributing to "scalability" in the operational/performance sense.

Like Erlang, Scala provides Actors with mailboxes for messages passed to them. Conventionally, a programmer has her Actors process received messages with a pattern match. Odersky notes that the Actor features of Scala are libraries, not core language features, and argues that this is a testament to the strength of libraries and an extensible approach to language design. Actors in Scala are implemented atop the FJ Framework; the event-driven flavor of Actors (using react) scale up to hundreds of thousands of processes in tests.

The next problem to be solved for Scala is maintaining expressiveness while keeping programs safe and verifiable. A project on "pluggable type systems" is underway, but is unlikely to yield changes to Scala for several years.

Odersky notes a number of companies working in Scala: Google, Buy-a-Feature, Gump-It, Joberator, EDP Training, Reaktor Innovations, Sygneca. Scala tools are improving, from the language's own complier, background complier, and interactive shell to testing frameworks and plugins for all the major Java IDEs. Rounding out, the "official" Scala book will be coming out soon.

Question time!

Are Scala classes open? No, because that defeats static typing. But: mixins, etc.

Does calling into Scala code from Java necessitate reflection? [Answer not captured, sorry.]

How stable is the language and what's the path for future features? We've agreed to slow the development of the language. There will be a standard distribution and an experimental branch.

Is there a project to add Scala support to the Java compiler so you can mix the two languages in the same project/file? Yes! Eclipse has this working to some degree, other projects are underway.

Because of the unconference format, more questions will be asked of Martin throughout the day and the Q&A session was cut short.

Dec 25, 2007

Comparative Languages: Community is Everything

Over the past year one of my main technical interests has been exploring and comparing various programming languages. I'm not exploring the boundaries of linguistic obscurity like Steve J; I want to borrow ideas from other languages that I can incorporate in my daily work and evaluate potential future tools. (Any programmer who's satisfied with her tools isn't paying attention.)

There are endless metrics against which one can judge a programming language: the performance of programs, subjective readability, availability of libraries for common tasks, availability of quality language primitives, &c. In my explorations, however, nothing matters more than the community surrounding a language when determining its overall success.

A Happy Place

People program in Ruby because it's fun; it makes developers (of a certain persuasion) happy. Happy developers evangelize, and thus a friendly, productive community has grown up around Ruby and Rails over the last several years. Coming from the world of Java or PHP, the Ruby community is some brilliantly green grass. Rubyists tend to be pleasant, articulate, and helpful. That people write cogently about Ruby is perhaps the community's greatest strength, and one that will surely be tested as the community swells with bulk-rate developers.

A Chilly Place

2007 has been a big year for the visibility of functional languages like Lisp, Haskell, and OCaml, but no accessible community has yet formed around any of them. Indeed, by some accounts the functional language community can be downright hostile. In my experience, functional programmers can be a dry and academic lot. Languages don't take off until someone with a sense for humane aesthetics starts evangelizing in a format that people enjoy consuming. While Paul Graham has waxed poetic about Lisp, it'll take more than his essays to warm people up to the functional way of thinking.

A Scala Place

Former Java heads with an eye towards the more practical aspects of functional languages are flocking to Scala which, as mentioned in my previous post, runs atop the JVM. Scala, much like ECMAScript 4, has the potential to be the Next Big Language. It's got constructs to handle most any style of programming, a superb approach to concurrency, and much more. What may tie Scala down are its deep roots in the Java community.

That Scala builds on the strengths of the JVM and the plethora of available Java libraries makes good pragmatic sense. It also means that knowing Scala means knowing Java, and in turn dealing with the Java community. That means things like ugly documentation, forums and IRC channels filled with miserable code and incomprehensible language, Sun's endless cheesy marketing, and a pervading sense of enterpriseyness. Java isn't cool, it's Serious Business.

Converts from Java to Ruby haven't made the switch because Ruby is faster, or better at concurrency, or deployable in a broader range of environments, or better supported. They haven't reinvented the wheel in Ruby with new build systems, documentation setups, and libraries because doing so makes good pragmatic sense. Quite the contrary, on all counts. Developers have chosen Ruby because of the community. They've reinvented the wheel so many times over to keep tools within the Ruby community, where things are nice.

None of the above hassles will deter those familiar with the Java community to dive into Scala, and talented developers are already doing so. For developers that have had the pleasure of largely avoiding Java for the past few years, Scala is a harder sell. For us, the Java community is an albatross around Scala's neck. The question is whether Scala's benefits will outweigh that negative.

Dec 20, 2007

Big in 2008: Dynamic Languages atop High-Level Languages

If you'll allow for some generalization, the mainstream programming language stack basically looks like this:

Stack 1


Dynamic, interpreted languages at the top, nitty-gritty close-to-the-machine languages at the bottom. It hasn't always been this way, of course, but that's certainly been the stack for the last few years. Lately, though, something's been changing. There's a new layer on the stack, and now it looks like this:

Stack 2


Now we've got dynamic languages running on top of interpreted languages, and in some cases atop other dynamic languages. Some of these languages at the top of the stack have been in development for years, but my assertion is that 2008 will be the year that they see increased adoption across the industry.

Why?

Abstraction means increasing returns in developer productivity. Implementing a non-trivial language in C is a hellacious challenge; implementing it in a language like Java, apparently less so given the relatively rapid time-to-market of projects like JRuby. Environments like the Java Virtual Machine (JVM) and and Microsoft's .NET Common Language Infrastructure (CLI) are written for performance and portability. Building on a powerful VM or runtime environment allows a language implementor to inherit some powerful traits.

If this trend of was only happening on the JVM and CLI it would be notable but hardly worth going on about. What made me take pause was the emergence of Nu and ruby+objc. Nu looks like Lisp, and ruby+objc will look like Ruby, but the goal is the same: build a language that gets the Objective-C runtime to do the dirty work and focus on a pleasurable syntax and higher-level abstractions. Implementing the language directly on the runtime has the added benefit of doing an end-run around the problems of bridging languages.

Portable runtimes are big engineering efforts, typically the province of the Suns, Microsofts, and Apples of the computing world. It's understandable that big engineering shops would gear their runtimes towards industrial-strength languages like Java, C#, and Objective-C. But there are plenty of projects for which an industrial-strength language is an impediment, not a boon. Having access to a dynamic language on top of your favorite industrial-strength high-level language is an incredibly useful tool in a programmer's toolbox.

Proving the Trend

Runtime-by-runtime, here's a list of the languages at the top of the stack in 2008:

JVM
  • Scala - "a general purpose programming language [...], concise, elegant, and type-safe, [...] smoothly integrates features of object-oriented and functional languages"
  • JRuby - Ruby for Java
  • Jython - Python for Java
  • Groovy - "an agile and dynamic language for the Java Virtual Machine"
  • so very many more
.NET CLI
  • Boo - "a new object oriented statically typed programming language"
  • IronPython - Python on the.NET CLI
  • IronRuby - Ruby on the .NET CLI
  • F# - ML on the .NET CLI, "provides [...] type safety, performance and scripting"
  • really quite a few more, albeit not as many as on the JVM
Objective-C
Even more out there, here are languages implemented atop dynamic languages, some experimental, some practical:
  • Puppet - a "simple declarative specification language [...] written entirely in Ruby" for systems management. We're using Puppet to manage our new cluster at Twitter.
  • RLisp - a Lisp implemented in Ruby
  • Bus Scheme - "a Scheme written in Ruby, but implemented on the bus!
  • PyPy - Python implemented in Python (but it's actually way more than that)
At any rate, something to watch in aught-eight. I'd be interested to hear if any of the above languages are on your radar.

Oct 20, 2007

Getting Started with Scala and Liftweb

I've had an ongoing dilemma when sitting down to work on Peeramour. I could write the site in Rails in an afternoon, which is tempting. Thing is, I write Ruby/Rails code all day. I'm burnt out on it. Plus, I'd like to learn something new if I'm going to be programming in my off-hours.

Looking around the landscape of web application development, only one technology has really caught my eye: Liftweb, a framework built with the Scala language, which in turn runs on the JVM. I like that both Lift and Scala borrow successful ideas where appropriate. I like that both are built with performance, scalability, and ease of deployment in mind. Most of all, though, I like that neither Lift nor Scala is particularly sexy. It's just ugly enough to work.

Getting started with Scala on Mac OS X 10.4 is pretty straightforward:

  1. Download either the Gzip or Bz2 Unix tarballs from the Scala downloads page.

  2. Unpack your tarball of choice and put it somewhere sensible like ~/src/scala.

  3. Add the path to the bin directory to your shell's PATH.
You should now be able to run scala, type in some arithmetic expressions, that sort of thing. Hunky-dory. Now let's get Lift-ed.
  1. If you've got MacPorts (and you should), do a sudo port install maven2. Maven is a build system for big honkin' Java projects, and it takes care of grabbing dependencies and all sorts of junk.

  2. Now, grab the actual liftweb source from Subversion. The packaged versions are old.

  3. Pop into your new liftweb directory and do a mvn install. It'll take a few minutes while Maven grabs and builds this and that, but at the end you'll have built all the examples and whatnot.

  4. A basic blog built in liftweb can be found in sites/hellolift. Head over there, run maven jetty:run and you can poke around at a real actual Lift app.
Instructions for starting your own liftweb project can be found at the developer's blog. Though that post was written in June, the invocations therein still work.

I'm used to working in TextMate, but the only Scala bundle out there is pretty immature. I'm not after a bunch of IDE fanciness, but good syntax highlighting is a must. I imagine things are more hospitable in Eclipse, but that way perdition lies. My next step is to check out the vim support.

Now that I've got a reasonable working environment, tomorrow I'm going to take a stab at some actual code.