Brevity

Ted Neward compares a handful of different languages (Java, C#, VB, Scala and Ruby) with respect to how much code it takes to express a simple person concept:

A Person has a first name, a last name, and a spouse. Persons always have a first and last name, but may not have a spouse. Persons know how to say hi, by introducing themselves and their spouse.

Of course, I couldn’t help but compare it with neptune which (sigh of relief) compares favorably:

class Person {

readable String first_name, last_name;
readable Person spouse;

Person(String -> first_name, String -> last_name,
Person -> spouse = null);

String to_string() {
if (spouse==null) return "Hi, my name is ${first_name} ${last_name}";
else return "Hi, my name is ${first_name} ${last_name}"
", and this is my spouse ${spouse.first_name} ${spouse.last_name}"
;
}

}

Personally, I think that is more readable than both scala and ruby but of course I’m biased. Here’s a short run through of what’s going on in this code.

The modifier readable means that the field is given a read accessor. So instead of writing

readable String first_name;

you would get the exact same result if you wrote

String first_name;

String accessor first_name {
return first_name;
}

In the Person class above, unlike C, C++ and Java, spouse.first_name does not just read the field first_name in spouse directly but calls the read accessor called first_name. In neptune, hidden or no modifier defines a field but doesn’t create accessors, readable gives it a read accessor, writable gives it a write accessor and visible gives it both 1.

The Person “method” is the constructor. The arrow notation means “assign this argument directly into the specified field” and the = null specifies a default value for a parameter (as in C++). The constructor above could be written alternatively as the following, which is completely equivalent but, I think, a lot less clear:

Person(String _first_name, String _last_name, Person _spouse) {
first_name = _first_name;
last_name = _last_name;
spouse = _spouse;
}

Person(String _first_name, String _last_name) {
this(_first_name, _last_name, null);
}

If you want to go completely nuts you can even expand the constructors into two static new operators and an instance initializer. And once you’ve done that it’s pretty easy to see that basic neptune is almost just smalltalk with a warped syntax. But I digress…

By the way, if you’re not into the whole type thing you’re free to leave out the types

class Person {

readable var first_name, last_name, spouse;

Person(-> first_name, -> last_name, -> spouse = null);

to_string() {
if (spouse==null) return "Hi, my name is ${first_name} ${last_name}";
else return "Hi, my name is ${first_name} ${last_name}"
", and this is my spouse ${spouse.first_name} ${spouse.last_name}
;
}

}

[1]: Why didn’t we use public and private instead of visible and hidden? Well, our modifiers mean something different from Java or C++’s do in almost all positions where they occur. We thought that it was important to avoid misleading people by emphasizing the similarities, which I think we would have if we had used public and private, and instead emphasize the differences by choosing completely different keywords.

Leave a Reply

Your email address will not be published. Required fields are marked *


*