Skip to content
This repository has been archived by the owner on Sep 9, 2019. It is now read-only.
Timm Friebe edited this page Jun 2, 2013 · 6 revisions

The XP Language's syntax is based on PHP. If you're coming from there as a background, the following will given a short overview of the differences:

public class HelloWorld {
  public static void main(string[] $args) {
    util.cmd.Console::writeLine('Hello World!');
  }
}

The things you will have noticed are:

  • Classes may also have modifiers.
  • The extends Object is optional and added by the compiler if omitted.
  • The keyword function is gone and replaced by the return type. Because the main() method does not return anything, we use void.
  • An array type is written as component[]
  • Variables still have dollar signs. This makes it easy to spot them, that's why we've decided to keep this!
  • Fully qualified classnames are written with dots.
  • The object operator is also a dot (at the same time, the string concatenation operator is now the tilde, ~).

Literals

XP Language has literals for primitives, the void type, arrays and maps:

$i= 1;                // an integer
$d= 1.0;              // a double
$s= 'string';         // a string, inside single quotes
$s= "string";         // a string, inside double quotes, may contain escape sequences
$t= true; $f= false;  // boolean values true and false
$n= null;             // NULL
$a= [1, 2, 3];        // an array of any type as values
$m= [one: 1, two: 2]; // a map consisting of string keys and any type as values

Reference types

The types in XP Language are declared by the keywords class, interface or enum.

Class

Classes may have a parent (if omitted, defaults to lang.Object), can implement as many interfaces as they wish and declare members. Classes can be abstract (means abstract parts of their declaration need to be implemented by subclassing), or final (meaning they cannot be subclassed).

public class Person extends Identity implements Comparable {
  protected string $name;                   // A field

  public __construct(string $name= null) {  // The constructor
    $this.setName($name);
  }

  public void setName(string $name) {       // A method
    $this.name= $name;
  }

  // ...
}

Interfaces

Interfaces can optionally have multiple parents, and may declare member methods, which need to be declared by classes implementing them.

public interface Closeable {
  void close();
}

Enums

Enums provide type-safe enumeration of values, can define a parent which must also be an enum (defaults to lang.Enum), may implement interfaces, and can optionally have any member a class can also have. There are two types of enums: Simple and abstract enums.

This simple enum declares the members Weekday::MON through Weekday::SUN with the ordinals 0 .. 6, and defines an accessor to easily test for weekends:

public enum Weekday {
  MON, TUE, WED, THU, FRI, SAT, SUN;  // The enum members

  public bool isWeekend() {
    return $this.ordinal > self::$FRI.ordinal;
  }
}

This abstract enum declares two members, plus and minus, which implement the abstract evaluate() method inside the enum itself.

public abstract enum Operation {
  plus {
    public int evaluate(int $x, int $y) { return $x + $y; }
  },
  minus {
    public int evaluate(int $x, int $y) { return $x - $y; }
  };
  
  public abstract int evaluate(int $x, int $y);
}
Clone this wiki locally