Oxygene has come so far from where Delphi left the Pascal language when it stopped innovating in the late ’90s that it’s hard to provide a simple and concise overview of what’s “new” in Oxygene for Delphi developers – there’s just so much.
This topic will try to provide brief sections on most of the major improvements and new language features that Oxygene provides, covering them with a brief introduction and then linking off to the main language documentation where you can find more details.
Pretty much all of these features, with the one exception of Generics, will be new to you, whether you are coming from Delphi 7 or a more recent Delphi version such as XE7, because the Delphi language really hasn’t changed much over the past 15 years.
While pretty much all code in Oxygene lives inside types, this first section looks at new kinds of types that Oxygene introduces (such as tuples and sequences), and fundamentally new things you can do with types (such as nullability). Let’s get started.
Sequences are a special type that exist on a similar level to Arrays, and can also be thought of as a collection of elements.
Different from arrays, sequences do not imply a specific form of data storage, but can represent any collection of elements that is accessible in a specific order. This could be an array (and as a matter of fact, all arrays can be treated as a sequence) or a different data store.
One major advantage of working with data via sequences is that your code can start to work on the first items of a sequence before the entire sequence has been generated. Your code might even stop working the sequence at some point, and the remainder of it never will be generated. This allows for some pretty powerful use. For example, you can query a large database table, and have rows fetched as you need them. You could even have an infinite sequence that generates all the digits of Pi, and choose to process only the first 10,000.
Sequences are defined with the
sequence of keyword combo, parallel to arrays:
var lCustomers: sequence of Customer;
Sequences can be looped over with the regular
for each loops that recent versions of Delphi have also introduced, and they also work great with LINQ and Oxygene’s
from expression syntax for LINQ.
Aside from many APIs in the frameworks that already expose sequences, the Iterators and For Loop Expressions features discussed below help you define your own sequences in Oxygene.
Tuples are the second new kind of “container” type introduced by Oxygene. You can think of a tuples as a group of two or more strongly typed variables that can be used and passed around in combination – for example passed into or returned from a method.
Each member of a tuple can have a different, well-defined type (for example, you can have a tuple of a number and a string), but different than a
record, members of a tuple have no individual names. Commonly, tuples are used in a more light-weight fashion, in places where declaring an explicit record type somewhere else would seem overkill.
Types are defined with the
tuple of keyword combo:
var lError: tuple of (Integer, String) := (404, "Page not found")
You can access individual members of a tuple using their numeric index, such as
lError.0. You can also assign tuples back into individual variables by using a tuple literal on the left side of an assignment:
(lCode, lMessage) := WebRequest.GetError();
A Future Type is variant of a type that promises to have a value at a later time, but that value might not be calculated or obtained yet, and may be derived either asynchronously in the background, or the first time the future’s value is accessed.
Any ordinary type known in Oxygene can be used as future by prefixing its type name with the
var lCount: future Integer := lSomeSequence.Count; // the count of a sequence might be costly to determine
An ordinary future as in the example above will be evaluated the first time the value is accessed. All future access to the variable will use that same value. In essence, the future enables
lCount to be referred to multiple times, but ensures it won’t actually be calculated until (and unless) it is actually accessed. Within any subsequent code,
lCount can be used just as if it were an ordinary Integer, so it could for example be used in arithmetic expressions such as
Futures really shine when used in combination with
async expressions, as covered below. A future initialized with an asynchronous expression will start calculating its value in the background automatically, so it might already be available when first accessed. As such, futures really help writing parallelized code that can take advantage of multi-core CPUs, with very little work.
Anonymous Classes provide a syntax to quickly define and instantiate a simple class – commonly containing only data, but no code – from inside the code that will use it. Anonymous classes are not often used on their own, but they really shine when used in combination with Sequences and LINQ, as they enable you to filter down or combine data from sequences into new objects on the fly.
For example, as you are processing a lot of Customers and their Orders, you might want to generate a new list that contains each Customer and their total order volume, and then loop over that list. Anonymous classes make that easy without having to clumsily define a class for this. In particular, the
select clause of LINQ
from expressions will commonly define new anonymous classes.
Anonymous classes are defined using the
new class keyword combo:
var lCustomerData := new class(CustomerID: lCustomerID, OrderVolume: lOrders.Sum);
Anonymous Interfaces are very similar to anonymous classes, and are used to define a new class inline that satisfies (i.e. implements) a given interface. This is commonly used on the Java and Android platform, where rather than Delphi- or .NET-style Events, controls usually are assigned a delegate object that implements a given interface in order to receive callbacks when events happen – such as to react to the click of a button.
Anonymous interfaces allow you define such a class inline and implement one or more handler methods without having to implement the interface on the containing class (which can be awkward if you need to provide different handlers to different controls – for example two different click events on two different buttons).
You can think of anonymous interfaces as an extension or a more sophisticated version of Anonymous Methods. In fact, an anonymous method is considered the same as an anonymous interface with just one method.
Anonymous classes are defined using the
new interface keyword combo:
fButton.delegate := new interface(OnClick := method begin
// handle the click here
The Partial Types feature allows the definition of a Class or Record to be split across two or more source files. This is mainly used for three different types of scenarios:
- Complex or very large classes can be split up to keep the individual code files more manageable.
- Classes that are shared across platforms (for example via Shared Projects can have one part that’s shared, and another that provides platform-specific logic, without needing excessive
- Some UI frameworks, such as WinForms and WPF will use one part for user code, while a second part is maintained by the visual designer or build tool chain.
In Oxygene, like Delphi, simple value types that are stored on the stack will always have a value (a default of 0, if not otherwise initialized), while reference types (mostly Class) that are stored on the heap will be
nil unless initialized.
Oxygene, however, provides a way to override this. A variable, field or parameter of value type can be marked as
nullable type to indicate that it will default to (and can be assigned)
nil. Similarly, a variable of reference type can be marked as
not nullable, causing the compiler to enforce it to always be assigned a valid value and never be
Most interestingly, and unique to Oxygene and the other Elements languages,
nullable value types can be used in code, including arithmetic expressions, just as their regular counterparts. The Nullability will filter through, so that any expression using a
nullable type will in turn also be nullable – and in true tertiary boolean logic, an actual
nil value in an arithmetic expression will turn the whole expression
var x := nullable Int; // nil
var y := 5;
var z := 10*x+y; //z will be nullable, and nil
You can read more about nullability here.
Mapped type are a unique feature of the Elements compiler. They let you create compatibility wrappers for types without ending up with classes that contain the real type. The wrappers will be eliminated by the compiler and rewritten to use the type the mapping maps to.
When working with Oxygene, you will most commonly use mapped types (for example as provided by the Sugar cross-platform library). Using mapped types is seamless, and they behave just like regular non-mapped types.
You will not often need to implement mapped types yourself, but for when you do, Oxygene – like RemObjects C# and Swift – provides a syntax for implementing mapped types when needed, with the
Please refer to the Mapped Types topic in the Language Concepts section for more details.
That covers actual types, and as you see, Oxygene has quite a lot to offer. Next, let’s have a look at what you can do within those types (and in particular, Class or Record. It’s also worth mentioning that in Oxygene, Records are elevated to be pretty much as powerful as classes: In addition to fields, they can contain properties and methods, just like their siblings.
Pretty much the only difference between the two kinds of types is that classes are heap based – they get created in memory as needed, and variables refer to their memory location. More than one variable can point to the same class instance, and you can pass class instances all around your program. Records are stack based and value types. Two variables of record type will always point to unique copies of the record, and passing a record as parameter or assigning it to a second field or variable will create a copy of its data.
Fields in classes and records work and behave pretty much as you know them from Delphi. The only new feature for fields is that they can be marked with the
readonly directive, which indicates that they can only be written from the Constructor or via an initializer, but are then immutable.
Fields can also be initialized in line, and when they are, their type can be omitted if it can be inferred from the initial value.
fCount := 5; readonly; // fCount will be an Integer
Just like fields, Properties in principle work as in Delphi, but as mentioned above are also supported in Records, not just Classes.
That said, Oxygene vastly expands the syntax for declaring properties, making them a lot more convenient to define and work with. All of these features are covered in detail in the Properties section.
- Like fields, properties can me marked
- Like fields, properties can be initialized inline.
- Properties can be declared without
write clause, and will automatically be backed by an implicitly created field.
- Properties themselves can be marked
virtual and be overridden, which is cleaner than relying on virtual getters/setters as Delphi does.
- Properties can be defined in Interfaces.
- Properties can define different visibility for the getter and setter, for example letting you declare a property that is
public readable but only
protected writable, which can be very powerful.
- Properties can be marked as
locked to synchronize their access to be thread-safe.
- Properties can be marked to generate Notifications when they change, via the
- Properties can be marked as
lazy and have their initialization deferred until they are first accessed.
- Properties can use more complex expressions than just a field or method name for their
Methods also work just as in Delphi, and are supported in Records as well, not just Classes. As mentioned in the Minor Differences topic, Oxygene introduces a new
method keyword that we recommend to use for methods, instead of the old
function keywords. It emphasizes the Object-Oriented nature of Oxygene, and deemphasizes the largely irrelevant difference of whether a method returns a value or not. But
function still work as well, in Delphi Language Compatibility Mode.
But once again, Oxygene expands the syntax for declaring methods, all of which is covered in detail in the Methods section.
- Like properties, methods can be marked as
locked to synchronize their access to be thread-safe.
- Methods can me marked as
async to indicate that they will automatically execute in the background.
async methods with a return value will return a Future.
- Methods can me marked as
empty if they are placeholders that perform no function. This saves creating an empty method body.
- Methods can be marked as
inline, and their logic will then be embedded into the calling code for performance optimization.
In Oxygene, methods can use a new “multi-part method name” syntax that embraces Cocoa naming conventions (but is available on all platforms, and for all three languages) and makes for more readable and expressive method calls. You can read more in the respective section in the Methods topic.
Methods can also define pre- and post-conditions to validate their arguments and their results, which is covered further down on this page and under Class Contracts.
Iterators are a special kind of method that makes it easy to implement dynamically generated Sequences. Marked with the
iterator directive, an iterator method can write regular linear code that can use the
yield statement to add values to the sequence.
yield works similar to
exit in that it returns a value, except that the execution flow of the iterator method keeps going, because the returned value is just one of many that make up the final sequence.
Multi-Cast Events and Blocks
Oxygene introduces a new kind of member for classes and records: Events. While in Delphi events are essentially properties of a special type, and thus get no special syntax, events in Oxygene are fundamentally different and separate from regular properties, and are defined with the
Events are multi-cast, meaning that more than one handler can be assigned to an event using the
+= operator that Oxygene introduces exclusively for events. When the event is triggered, all assigned handlers will be called.
Multi-cast Events are almost exclusively used on the .NET platform, since the Cocoa, Java and Android platforms have different default mechanisms to deal with this concept – such as the Anonymous Interfaces discussed earlier on Java, or more traditional delegate classes on Cocoa. But the Event syntax and infrastructure is nonetheless available on all three platforms, should you wish to use it.
Finally, Oxygene allows you to define Custom Operators for your classes and records, allowing them to participate naturally in arithmetic expressions. For example, you can define the
+ operator for a record representing a Complex number or a Matrix, allowing code that consumes the new record (or class) to seamlessly add two values together.
You can read more in the Custom Operators section.
We’ve now covered both types and their members, so next, let’s take a look at what Oxygene lets you do inside those members, most particularly Methods-like members, in terms of the kinds of Statements you can write.
Inline vars and Type Inference
Most prominently, Oxygene does away with the need for an explicit
var section at the top of each method where all the method’s local variables need to be declared.
Instead, Oxygene lets you declare variables throughout the flow of your method where they are needed, with the new
var statement. This makes code easier to understand, as variables can be declared closer to where they are used, and even inside nested scopes, such as
if blocks or Loops.
More importantly, the
var statement supports type inference, so you can, for example, assign the result of a method call to a new local variable without restating (or even knowing) the exact type. Variables defined with inferred type will of course still be strongly typed.
Type Inference is of course especially important when working with Anonymous Classes discussed above, since these classes don’t even have a known type name that could be explicitly stated. Type inference is the only way to declare a variable holding such a type (or a Sequences of such types).
Mostly a curiosity but handy at times, Oxygene introduces a new loop type that runs indefinitely, with the
loop keyword. A
loop loop, also called an infinite loop, has no pre-determined exit condition and keeps running until it is broken out of with
While not used often,
loop does make for cleaner code and lets you avoid awkward and unintuitive
while true or
repeat until false loops.
Improved For Loops
for loops have also bee greatly expanded in Oxygene.
For one, Oxygene adds a new
in variation in addition to the trusted
for each loops run over all members of a collection, Array or Sequences, without your code having to maintain an indexer manually. (More recent versions of Delphi have adopted this loop style as well, so you might already be familiar with it.)
for each loops also have two advanced syntaxes.
- Via the
index keyword, you can introduce a second loop variable that keeps track of the count of loop iterations, without you having to increment the variable yourself. Essentially,
index gives you the best of both
for each and
to loops, in one.
- Via the
matching keyword, you can limit the loop to only execute for those members of a collection that are of a specific sub-type.
Currently on .NET only, both loop types can also be made to run multiple loop iterations in parallel on different threads, via the
Exception Handling has been expanded over Delphi’s in two ways:
- A single
try block can be followed by both a
finally block and one or more
except blocks. There no longer is any need to nest two
try blocks just to leverage both types of handler.
except blocks can be expanded using
where clauses to further filter which exception a given block will catch, using criteria other than just the mere exception type.
Advanced Case Statements
Oxygene expands the
case statement to be more flexible.
case statements can work on strings (smartly using a hash table in the background for efficiently finding the proper case to execute). This not only saves code over needing to write multiple
else if statements, but is also faster.
case statement can also execute different branches based on the type of its parameter, via the new
type of syntax.
Refer to the
case Statements topic for more details.
Similarly to the
locked directive on Methods and Properties already mentioned above, the
locking statement can protect a section of code against parallel execution on multiple threads, making it very easy to write code that is ready for parallelization. Via its parameter, the
locking statement gives you flexibility for how granularly to synchronize execution – for example per instance, or globally.
While Oxygene uses GC or ARC on all platforms and you do not usually need to worry about memory and object lifetime management, sometimes your code will interact with external resources (such as file or network handles) that do need to be released in a clean and timely fashion.
using statement allows you to write a block of code that will run and make use of a specific object and automatically dispose of the object at the end. Essentially,
using is a convenient way to encode a
finally block that makes sure your object and external (“unmanaged”) resources get cleaned up.
With statements out of the way, let’s look at some of the improved Expression types Oxygene provides.
Small but immensely powerful, the Colon (
:) Operator is a team favorite of all the features in Oxygene.
Delphi and Oxygene normally use the Dot (
.) operator to access members such as Properties or Methods of a class reference. This is something so natural and so frequently done, we mostly don’t even think about this as a special expression.
When trying to access a member of a class reference that happens to be
nil, an exception is raised. In Delphi, that is the dreaded Access Violation straight from the CPU, in Oxygene it’s a Null Reference Exception, often chummily called “NRE”.
NREs are great when they happen on truly broken code, as they report the failure in a clean and obvious manner. But oftentimes it would be nice to be able to write code that doesn’t need to care if an object is
nil or not. That’s where the Colon (
:) Operator comes in.
If you use
: instead of
. to call a member, Oxygene will automatically check whether the object you are trying to call into is valid or not. If the object is valid, the call will proceed as normal, same as with
.. But if the object is
nil, then rather than raising an NRE, Oxygene will just skip the call altogether and return
nil as the result.
Consider this example:
var lKnownSiblings := lSomeObject.Parent:GetChildren();
This code will call the
GetChildren method of the object from the
Parent property of
lSomeObject. But what if
Parent is not assigned (for example because data is incomplete, or because
lSomeObject is the root of the hierarchy)? Because the code uses
:, the call to
GetChildren will simply be omitted, and
lKnownSiblings will be set to
The Colon (
:) Operator allows you to write code that’s simpler (often avoiding many nested
if assigned(...) checks) and less error prone.
Double Boolean Comparisons
Double Boolean Comparisons allow you to compare three values in one step with a ternary operator – for example to check if a given value falls between two boundaries.
if 5 <= Count <= 10 then writeLn('between five and ten');
[Lambda Expressions] provide a convenient shortcut syntax for writing short Anonymous Methods without the overhead of a full
end declaration. Lambda expressions are commonly used for single-statement methods, where they consist of an (optional) parameter list, the special
-> operator, and the single statement. For example:
var lFives := lMyCollection.Where(x -> x.Value = 5); // filter list to items with value 5
Lambda expressions can be used anywhere anonymous methods can be used – for example as event handler assignments or as Block parameters to methods. One very common scenario, as shown in the example, is to use them with the LINQ query operators.
if expressions take the regular
if statement and allow it to be used for expressions. As such, the
if expression evaluates a condition, and then returns one value or the other.
var lLabel := if lList.Count = 1 then 'Item' else 'Items';
case allow the regular
case statement syntax to be used as an expression to return a conditional value:
var lCountString := case lList.Count of
else 'more than i can count';
For Loop Expressions
You are probably seeing a pattern here. For Loop Expressions are the expression version of the regular
for loop statement. Since a
for loop, by its nature, can run for many iterations, the result of a
for loop expression is a Sequences of values:
var lSomeEvenNumbers := for i := 1 to 100 yield i*2;
Similar to Iterators,
for loop expressions use the
yield keyword to add a value to the generated sequence. Also like iterators, the value of a
for loop expression merely represents the functional logic for generating the sequence. The loop code does not actually run until the sequence is enumerated.
async expressions allow a statement or an expression to be executed and evaluated asynchronously on a background thread, returning a Future Type.
async expression will return immediately, and execution will begin in the background (immediately, or once a free thread is available based on system resources).
Await Expressions (.NET)
Available on .NET only, the
await expression construct can be used to “unwrap” otherwise asynchronous code so that future results can be dealt with in a linear fashion. Under the hood,
await will break the method into different parts, scheduling them to be executed asynchronously once the awaited actions have been completed.
Please refer to the
await Expressions topic for more details.
From (LINQ) Expressions
A huge topic on their own,
from expressions provide a rather sophisticated sub-language that allows you to use an SQL-like syntax to work with Sequences of objects in a strongly-typed fashion. They form the basis of LINQ support.
var lFilteredData := from c in lCustomers
where c.Name.StartsWith('O') // filter by name
order by c.DateOfBirth // order by date
select c.Name, c.Address; // and return only two fields
// via a new anonymous class
Note: Although LINQ technology originated on .NET, Oxygene makes it and
from expressions available on all platforms.
“is not” / “not in”
Oxygene expands the standard
is type check operator and the
in operator that checks for membership in a set to allow for more natural use with the
if not (x is Button) then … // traditional Delphi
if not (5 in MySet) then … // traditional Delphi
if x is not Button then … // Oxygene
if 5 not in MySet then … // Oxygene
Last but not least, Oxygene introduces a major language feature called Class Contracts that allows you to write self-testing code in a “Design-by-Contract” fashion.
Class Contracts consist of two syntax features:
Inside method implementations, you can add code to check for pre-conditions and post-conditions using the
ensure keywords, as shown in the “Method Implementation Syntax” section of the Methods topic.
On a class (or record) level, you can define Invariants that are used to define a fixed state the type must fulfill at any given time. This makes it easy to detect bugs where any method or property setter leaves the type in an inconsistent state.
You can read more about these features in the Class Contracts topic.
This article originally appeared on our new Elements docs site at docs.elementscompiler.com. Check out our whole section for newcomers from Delphi at docs.elementscompiler.com/Oxygene/Delphi