|
at 2010-10-10
in Language
by friebe
(0 comments)
Now that we know the basics about the XP language - how to write a "hello world" application, how to compile and run it, and after looking at the underlying type system, and have seen both is quite similar to PHP - let's explore the differences, the object and concatenation operators, changed foreach, array and map syntaxes, the removed function keyword, as well as the now syntactically supported annotations, chaining, enumerations, varargs, and finally, the finally block.
The changes The biggest change you'll notice right ahead is the object operator, which was changed from the C/C++-inspired -> to the dot, as common in most other programming languages nowadays.
$this.getClass().getMethod('toString').getName();After using it for a while, you'll find our it just reads (and types) more easily. Now for this to work, the concatenation operator also needed to be changed - it's now the tilde (~):
$s= 'Hello'; $s~= ' ' ~ 'World'; The operator for static invocations has stayed the same, though:
XPClass::forName($name); In the spirit of reducing the amount of characters to type, and also consistent with many other programming languages is the array and map definition syntaxes:
$l= [1, 2, 3]; // An array $m= [one: 1, two: 2]; // A map
The arrow operator (=>) has been replaced by a single colon.
Also changed is the foreach statement, which lists the iteration variables first, then the expression to iterate on, adapting C#'s style:
foreach ($method in $class.getMethods()) { ... } foreach ($key, $value in $map) { ... }
As stated earlier, the function keyword is gone, it's replaced by the method's return type (or void, for methods not returning anything).
The additions The XP language also brings lots of new syntax to support features "hacked" ontop of PHP in the past. Annotations are now written without the leading "#" sign (which is actually a one-line comment in PHP):
public class ProductTest extends TestCase { [@test] public void getProductId() { // ... } }Chaining is allowed at any place, like in PHP 5+, after member references and method calls, but also after new, and also with array offsets, and on primitives:
$this.getClass().getMethods(); $this.member.execute($this); new Date().toString(); $class.getFields()[0].getName(); 'hello'[0];
Type-safe enums are now also supported by a syntax:
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
...and without the need to extend the lang.Enum base class (the compiler still does this internally though!)
Variable argument lists are declared by the ... appended to a parameter type, and accessed by the parameter name instead of using the func_get_args() function:
public string format(string $format, var... $args) { // ... }
The finally block completes the try statement. Code written inside it is executed whether an exception is caught or not, and can be used for cleanup:
try { $file.open(FILE_MODE_READ); return $file.read(5); } finally { $file.close(); }
...and also in conjunction with catch.
|
Subscribe
You can subscribe to the XP framework's news by using RSS syndication.
CategoriesNews General PHP5 Announcements RFCs Further reading Examples Editorial EASC Experiments Unittests Databases 5.8-SERIES Unicode Language 5.9-SERIES
RelatedFind related articles by a search for «XP».
|