NewSpeak at JavaZone


Best presentation at JavaZone so far was Gilad Bracha’s talk about NewSpeak. I might be one of a few number of people who thinks this of course, since for most Java developers NewSpeak is quite out there. For language geeks though, it’s a gold mine of interesting ideas, realized in a very nice way.

So, what is it? Well, NewSpeak is a new language created by Gilad Bracha (who used to work as Language Theologist for Sun – meaning he was one of the theory geeks for Java). NewSpeak actually doesn’t have anything to do with Java. It doesn’t run on the JVM. It’s not written in Java. It doesn’t look like Java. In fact, it’s closest relatives are Smalltalk, Self and Beta. It runs on top of Squeak, but there are plans to make it run outside too – probably targeting V8 for this.

If you were just to glance at the language, it looks a lot like Smalltalk. Gilad has based the syntax on Smalltalk, but have no problem with adding or removing things to make more code more readable, more accessible, and so on. But as it turns out, many choices in Smalltalk happened to be there for a reason, and having them gives lots of benefits.

So what are the nice features of it (except that it’s based on Smalltalk?) Well, these are the things that I took notice of:

  • No global state. Really. No global state at all. So how does it work? How do you create a library for example? Well, a library is actually a method. That method will be called with something called a platform. This platform gives you access to common resources, but note that the platform is also an instance – there is no global state there. It also means that you can inject any kind of platform into a specific library. What really makes this powerful is that it allows security by capabilities. What it means is that since there is no global state, a library can’t get access to something unless you inject it into that library. Gilad uses the example of the File object. If a File class haven’t been injected into your library, you can’t actually use any files. Or if someone injects something that looks like a File object but only allows reading, the library can only read from files. Neat. Security just comes as a side effect of this design choice. And btw, dependency injection frameworks doesn’t exist in NewSpeak, since everything is injected in the language in the normal course of coding in it.
  • Scoped injected super classes. This one is a bit complicated to understand, but it’s really powerful. In fact, it looks a bit like categories. An example you say? Well, say a library has a Foo class, a Bar that extends Foo, and a Baz that extends Foo. Then a piece of code that uses this library have a Foo2 class that is a subclass of Foo. But the kicker is that in this piece of code Foo2 is called Foo. In NewSpeak this means that in reality, the super class of Bar and Baz will actually be Foo2 – but only inside of the piece of code where Foo has been reset to be Foo2. This is a side effect of using interfaces for everything in the system.
  • Mirror reflection. One of the problems with building a secure system that still have powerful reflection capabilities is that it’s quite hard to scope this functionality in a secure way. So NewSpeak solves this by doing the same kind of injection for reflection as it does for all other things. That means you can’t do reflection on a specific class by itself – you will have to get a Mirror object for that class. And the way you get a mirror is by calling a method on a Mirror class to get it. This means that to use mirrors you need to inject a mirror class. And since you can inject a ReadableMirror for example, this means you can handle security for reflection as you do everything else in NewSpeak. Really cool.

In conclusion, I really like what I see in NewSpeak. I look forward to when it’s released (it will be open sourced under Apache 2.0). It’s got some fresh new ideas about how to approach language design.

Read more at Gilad’s blog: http://gbracha.blogspot.com/, or at http://newspeaklanguage.org/.


8 Comments, Comment or Ping

  1. Tomas

    Interesting piece. With the scoped injected classes, what would happen if you passed an instance of Foo2 back into the library (or elsewhere)? Would it change back into a Foo or (more likely) still be a Foo2? On surface level it seems like it would be confusing to code to when you aren’t sure what you are going to be passed when your function accepts a Foo. What kind of use cases would this feature be useful for?

    I realise you are not a native speaker, but please try to get the conjugation of “have” right. I count at least four grammar nazi violations. :)

    September 18th, 2008

  2. “Language Theologist”? I thought he used to call himself Computational Theologist…?

    Mats

    September 18th, 2008

  3. Interestingly, Tim Sweeney (of Unreal fame) has proposed something akin to these “scope-injected superclasses” of which you speak, for game extension reasons. Thus, if you need extra behaviour on the Pawn class (at the root of the player / AI hierarchy), you can subclass it and propagate that new subclass automatically, rather than having to reimplement the world. It’s a kind of “horizontal subclassing”, or squeezing in an extra class between an ancestor and descendant without modifying all the descendants.

    You can read more about it on this thread:

    http://lambda-the-ultimate.org/node/1277

    The idea looks useful in other settings for e.g. mixing in proxy objects for testing, without having to add an extra layer of abstraction all throughout your code.

    September 18th, 2008

  4. markus

    But how does the language “feel”? I.e. when I used php it didnt really feel like a good language (good in the sense that I enjoyed writing in it. Of course you can finish apps or tasks with it just fine)

    September 18th, 2008

  5. I’ve seen this in somewhat of a private talk at Hasso-Plattner-Institute in Potsdam, Germany. Very interesting stuff.

    Also very interesting is that you can easily write classes that are entirely pure functional, or immutable. I don’t remember the exact mechanics, but it comes down to not implementing mutator methods for your fields. This means you can probably write and detect classes that are safe in multi-threading, and safe for message passing to other threads/processes.

    Through the no-global-state doctrine you can also get very nice multi-threading and multi-processing capabilities. IIRC they also plan to build some sort of Erlang-style message passing into the platform.

    I feel this has the ability to really create an innovative platform for software, very much like the recent work by Alan Kay and Ian Piumarta. And it’s about time – most of our platforms suck, in one way or the other.

    Markus: in its feel, it’s apparently very similar to Smalltalk, wrt syntax, class hierarchies, the development environment etc. I think they also adopt the image concept. One difference is that Bracha claims they have a very good foreign function interface, so it’s no longer a closed world like Smalltalk – you can use the platforms UI capabilities, for example.

    September 19th, 2008

  6. The talk from Hasso-Plattner-Institute in Potsdam is also available online:

    http://www.tele-task.de/page50_lecture3490.html

    September 19th, 2008

  7. Hi Ola,

    Thanks for the nice post! I confess the term “scoped injected superclasses” is new to me, so I’d like to rephrase this in my own terminology.

    Basically, there are two scenarios: the superclass is defined within your module, or it is being imported/injected from the outside. In the former case, you can always override it in a subclass. In the latter, you can use any suitable superclass, as long as you inject it under the correct name (“Foo” in your example), which is simply a matter of defining a suitable accessor. And you can still override things in any subclass of your module.

    In both scenarios, all subclasses of Foo are of course tied to the latest version. Clear as mud? The video from Potsdam may help, for those who have the time.

    September 21st, 2008

  8. More links to interviews and talks from JAOO over at:
    http://www.jroller.com/murphee/entry/what_newspeak_brings_to_the

    Having to pass in everything a class wants to use together with the message sending means that you get Capabilities for Security & Co.

    Also: I finally grokked the Parser Combinator idea from the Executable Grammars paper (and some of the Newspeak slides) – highly recommended.

    September 24th, 2008

Reply to “NewSpeak at JavaZone”