First steps with the XP Language

at 2010-09-24 in LanguageExamples by friebe (0 comments)

The XP language is not completely different from the way PHP works, it keeps the flexibility and the general "look and feel" while adding syntactical support for features PHP doesn't support or doesn't support in all versions the XP Framework runs on.

Like in the XP framework, the entry point is always a class. In their most simple form, these classes have a static main() method. An example:

  public class HelloWorld {
public
static void main(string[] $args) {
util.cmd.Console::writeLine
('Hello World from ', self::class.getName(), '!');
}
}
OK, this doesn't look too unfamiliar, does it?


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, ~).

Compilation
To run the above example, it first needs to be compiled:
  # Compile source to HelloWorld.class.php
$ xcc HelloWorld.xp
...

# Now run it, as usual
$ xp HelloWorld

The compilation process may produce warnings and errors. The latter lead to a failure, while warnings are only informational. Examples of errors are parse errors as well as syntactical and structural errors where the compiled code itself would be erroneous - like method bodies in interface declarations or unresolveable types.

Coming up next: The XP language's type system.



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES
Unicode
Language
5.9-SERIES

Related

Find related articles by a search for «First».