Thursday, October 05, 2006

Various new features

On the language and compiler status page, I still have some unimplemented features left that I did not explain here yet. I will explain as much as I can in this writing.

First 'initializer lists'. I will assume that you know initializer lists from C++. In hyper they are not very different. The only (semantic) difference is that Hyper allows fields to have an initializer in their declaration and C++ does not. In a constructor, to initialize a field the value in the initializer list is used if it's present. If it is not, then the initializer in the declaration of the field is used. No initializer there assumes the use of a default constructor to initialize the field. The syntax consists of lines starting with a colon, it resembles the way it looks in C++, although C++ requires it to be on one line and Hyper allows to spread it across multiple lines for readability. An example to illustrate all this (unfortunately this blog thing eliminates all indentation from my example):
class Test
var a : string = "SSS"
var b : real = 9.81
var c : int
var d : bool
var e : char
var f : * byte

procedure new()
: a("Cookie")
: c(16), f(new byte(13))
# Other code
end Test
In this example a gets the value "Cookie", b is initialized to 9.81, c becomes 16, d is initialized with its default constructor (which initializes it to false), f is initialized to a pointer to a byte with value 13, and e gives a (compile time) error because char doesn't have a default constructor (yet?).

Another feature: comparison operator chains. This allows the mathematical notation of comparison operators that are used together: 1 <= 20 = (19 + 1) < 75 etc. And this notation supports the meaning that is obvious to someone who has never programmed before; a = b = c is not the same as (a = b) = c, but is equivalent to a = b && b = c. The difference is that in a = b = c, all expressions are evaluated once (b is not evaluated twice as in a = b && b = c) and the order of evaluation is undefined. a is not necessarily evaluated before c, and the comparison of b and c is not necessarily after the comparison of a and b.

Hyper will also provide two kinds of enums: nominal and ordinal ones. For now only nominal ones are explained as I haven't really figured out a syntax yet for the other type. Enums are strongly typed in Hyper. They are not a named alias of constants of some numeric type. Nominal enums represent named, uncomparable values. They are in their own namespace so you need to explicitly refer to the enum values by the name of the enum. Enum values are just listed, separated by comma's or newlines. An example of an enum declaration:
enum Transportation
Car, Bus,
Airplane, Subway
An enum can be declared anywhere a class declaration is allowed. The first member is considered the default value for variable/field declarations. I am also looking for a different approach, that there is no default and that if you want one you will have to specify which one it is. Remember to use Transportation.Bus instead of just Bus, because explicit qualification is required.

That's it for this time. New features I still have to explain (later):
  • single inheritance
  • modules & imports (still requires some thinking)
  • copy procedures (not on the website yet)

No comments: