Haxe Evolves

The haXe programming language is very expressive and feels light weight thanks in part to type-inference, structural typing, enums, generics and other nice things. If you want to know about haXe, visit the official homepage. It has a reference, several tutorials and a host of libraries.

The short story is that haXe looks a bit like ECMAScript and Actionscript but is more expressive than any of them. The haXe compiler has multiple targets and platform targets (Javascript, Actionscript 2 bytecode, Actionscript 3 bytecode, Actionscript 3 source code, Neko).

In light of the recent announcement of the plans for haXe 2.0, I’ve thought a little about how to make the syntax simpler and more uniform.

The concepts that have to be aligned are

  • field
  • function
  • property
  • value
  • variable

The basis concept is the field.

A field can be either a value or a variable. A value field is single-assignment by nature. A variable field is many-assignment by nature.

A field can be either a normal var/val or a function.

A function field can be either a getter, a setter or a general function.

A property function (getter or setter) defines a computed field access or a computed field assignment. A variable property function cannot be rebound, but the setter property function is defined as a var while the getter property function is defined as a val. If there is only a setter the property can only be assigned to. If there is only a getter the property can only be read.

class Point
val x: Float;
val: y: Float;
val theta: Float { ….. }
var theta: Float { ….. }
val rho: Float { ….. }
val sub (a: Point): Point { ….. }
var out (): String { ….. }

To explain the class, line by line

  1. field `x´ of type Float
  2. field `y´ of type Float
  3. property getter `theta´ of type Float
  4. property setter `theta´ of type Float
  5. property getter `rho´ of type Float
  6. method `sub´ of type Point -> Point (function of type Point -> Point -> Point)
  7. method `out´ of type Void -> String

Notice that `out´ is a general method/function (indicated by parens) and its delegate can therefore be taken. Plus since it is a var, a new function can be assigned as the `out´ method.

Design principles

  • succint syntax
  • uniform syntax/semantics

Design decisions

  • public-by-default
  • no function keyword in function declarations
  • no immutable-by-default bias – both are possible and up to the programmer

There are many more things that can be improved (changed, if you will – may be improvements, may not be)

  • non-null is the default; nullability introduced with an option/maybe/cell type
  • maybe tuples and more pattern matching, not sure
  • even more enum power
  • better handling of enums in flash 9
  • more static checkking of certain properties
  • static import or package-level functions
  • operator overloading

Nonetheless, I’m most concerned about the core language, optimization and making the core features as powerful and expressive as possible – more than stuff like operator overloading and static import. Nicolas Cannasse will choose wisely, I’m sure.

For fun, let’s see how operator overloading could be expressed

class Point
val opSub (that: Point): Point { ….. }
val opAdd (that: Point): Point { ….. }

This is similar to how D handles it. A specialled named method is recognized as an operator function.

Tuples could be nice, although anonymous objects can do the same, assuming they have value fields and are allowed to be pattern matched against or the values can be gotton via an index (such as a[3]). On the other hand: the fewer types and the “stronger” types, the better.

That’s it. Comments welcome.


About xosfaere

Software Developer
This entry was posted in Haxe and tagged . Bookmark the permalink.

One Response to Haxe Evolves

  1. I agree with about everything said here… I can’t wait for haXe to introduce more language power as a meta layer to AS3!

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s