Javascript ain’t so bad

It is getting darker, the heat is almost bearable now. The phlegmatic programmer’s brain starts working again.

Extensive Javascript experience during the last month lead to the decision to add a new feature to Babel-17 v0.21 .  In Javascript, you can do the following:

var x = new Object()
x.a = 10
x.b = 20
x.c = 30

In Babel-17 v0.2 you could achieve the above only like this:

val x = object
  def a = 10
  def b = 20
  def c = 30
end

In Babel-17 v0.21, you can also write:

val x = nil
x.a = 10
x.b = 20
x.c = 30

which has exactly the same effect. How is this possible? Isn’t Babel-17 purely functional?

First, the keyword nil is just an abbreviation, we have

nil == object end

Second, the assignment x.a = t is short for:

x =
  begin
    val evaluated_t = t
    object+(x)
      def a = evaluated_t
    end
  end

The nice thing about the new feature is that it integrates seamlessly with the linear scope rules of Babel-17. For example, you can write

val x = nil
if some_condition then
  x.a = 10
else
  x.b = 20
end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: