-
Couldn't load subscription status.
- Fork 1
Syntax
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 Objectis optional and added by the compiler if omitted. - The keyword
functionis 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,
~).
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 valuesThe type literals for the above primitives, arrays and maps are as follows:
int $i;
double $d;
string $s;
bool $b;
void; // Only valid for return type
var $v; // Any type, including reference types
int[] $a; // An array of ints, var[] allows any component type
[:bool] $m; // A map with string keys and boolean valuesThe types in XP Language are declared by the keywords class, interface or enum.
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). Members can be declared using visibility operators public (anyone may access), protected (only the declaring class and subclasses have access) and private (access only the declaring class), and may also be made abstract or final with the same meanings as for classes.
public class Person extends Identity implements Comparable {
const string PREFIX= 'P'; // A constant
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 can optionally have multiple parents, and may declare member methods, which need to be declared by classes implementing them.
public interface Closeable {
void close(); // A method waiting to be implemented
}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);
}XP Language supports the if and switch flow control statements as well as two forms of the ternary operator:
If / Else:
if ($condition) {
// When condition is met
} else if ($other) {
// When other condition is met
} else {
// When neither condition is met
}Switch:
switch ($condition) {
case 1:
// When $condition == 1
// No break: Falls through
case 2:
// When $condition == 2
break;
default:
// When neither of the above is met
}Ternary:
$result= $condition ? 1 : 2; // Result will be 1 if condition is true-ish, 2 otherwise
$result= $value ?: 3; // Result will be $value if that is true-ish, 3 otherwiseXP Language supports four kinds of loops: for, while, do..while and foreach:
For:
for ($i= 0; $i < 10; $i++) {
// Runs ten times
}While:
while ($element= $elements.next()) {
// Runs until element becomes false-ish
}Do:
$element= $elements.first();
do {
// Runs until element becomes false-ish
} while ($element= $elements.next());Foreach:
foreach ($method in $class.getMethods()) {
// Iterates over all of the class' methods
}
foreach ($key, $value in $map) {
// Iterates over all of the given map's keys and values
}XP Language supports exceptions as the primary means of handling errors.
The most basic form of a try/catch-statement is as follows:
try {
throw new lang.IllegalStateException('Shouldn\'t be here!');
} catch (lang.IllegalStateException $e) {
util.cmd.Console::writeLine('Caught expected ', $e);
}Multiple catch blocks may be added, and multiple exceptions may be caught and processed in one catch block. A finally block may be added to be executed whether or not an exception was thrown:
try {
// Invoke an action which may throw an exception
} catch (lang.IllegalStateException | lang.IllegalArgumentException $e) {
// Handle ISE, IAE (or subclasses of any of the two)
} catch (lang.XPException $e) {
// Handle any exception subclassed from lang.XPException
} finally {
// Runs in any case
}Methods may declare thrown exceptions with a throws clause. This is used for documentation and reflection purposes only - callers need not catch and handle these exceptions.