fredag, januari 04, 2008

Keywords in languages

It's nice to see how the amount of people looking into Scala has really exploded lately, based on the rush of blog posts an discussion.

One of the things I find a bit annoying about Scala is the proliferation of keywords. Actually, this is something I really don't like in any languages. A language should be as keywordless as possible. Of course, such a vision goes against ease of implementation for language implementers, so there always needs to be a balance here. Coming from languages such as Lisp and Io, it's amazing how clear a language can be with a well chosen message passing or invocation model. In fact, both of those languages have zero keywords. That makes it incredibly nice to implement whatever you want.

Actually, Java has been quite good at not taking much more keywords than they had from the beginning, so I found it a bit annoying when I tried to build a fluent interface in Scala, and found out that the word "with" is a keyword. And it's a keyword in the strictest sense, meaning you can't use it in places where you can't use the "with" keyword anyway. So there is no way to implement a method named "with" in Scala. Annoying. It's just, the English connection words are so much more useful for method names, especially when you can use the methods in "operator" position. Then you just want to be able to use all these words.

So. If you design a language, make sure that you take care to actually add every keyword extremely carefully. If you can, make sure that keywords can actually be used for some things when there is no ambiguity. Of course, I'm not proposing the kind of madness you can do in some languages, where a statement such as "IF IF THEN THEN" is valid, since the first IF is a keyword, the next is a variable name, etc. But be reasonable about keywords. They are sometimes necessary, but not always as often as people believe.

6 kommentarer:

Paul sa...

Totally agree. I love how minimal Smalltalk was in number of reserved words: self, super, nil, true, false...

Okke sa...

Almost agree. It's a paradigm issue. When the intention of a language is to be as 'extensible' as possible, keywords can be show stoppers. But when that is explicitly not an intention, keywords can simplify and/or clarify a language in order to make the life of programmers more easy. Cobol for example is a pretty neat language. Especially nowadays when writing English seems to be an API designers ultimate goal.

Andreas Krey sa...

In my opinion there should be no keywords at all. (There is nothing special about 'five'.) Make them identifiers bound to a special meaning in the global scope instead. That way the parser interacts with the scoping system (as in C's typedef) but you get a place where you can later hook in macros. :-)

René Vincent Jansen sa...

Look at NetRexx for a language with no keywords at all. It gives one the certainty that code keeps running, as opposed to other languages where every new version is a new language.

Andres Testi sa...

I agree. I there are situations where keywords are not really needed in Scala:
+ modifiers would be implemented as annotations: @Override instead override, @Abstract instead abstract, @Sealed instead sealed, etc..
+ "while" should be a method in scala.Predef
+ "match" would be replaced by a method.

Whit no keywords it is possible to apply @deprecate allowing backward compatibility.

Landei sa...

In Scala you can always escape keywords using backticks:

def `with` { println("!!!")}

Not nice, but at least a workaround when you have name clashes...