Expressions are a constructs of code that represent a value. They are similar to Statements, but with a crucial difference: while regular statements must always stand on their own, expressions represent a value that can (sometimes must) be used in the larger context of a surrounding expression or statement.
For example "
5 + 3" is an expression, with a resulting value of
8. It makes little sense to write "
5 + 3" as a statement on it's own, but the expression can be embedded in a more complex formula such as "
(5 + 3) * 2", or in a statement such as "
x := 5 + 3;".
Some expressions can be used as statements, having their final value ignored. For example, a Method Call Expression might produce a result that can be part of a larger expression, but the same method can also be called as a statement.
Examples of Expressions
Expressions can be as simple as a literal value, such as the number
5 or the string
'Hello', they can be simple identifiers, referring to a Variable or calling a Method that returns a value, or they can be complex – well – expressions combining several expressions together to represent a new expression.
var five := 5; // a simple literal expression, '5', is used to initialize a variable var ten := 2 * five; // a literal `2` and a variable reference expression (`five`) are used as // operands to a larger expression that multiplies them if ten > 5 then DoThis(); // a variable reference expression is compared to a literal // expression and the resulting boolean expression is used to // conditionally execute the `DoThis()` method call expression
In the above examples, you have seen three kinds of expressions:
- Literals – an actual hard-coded value.
- Binary Expressions – combining two operands with an operator such as
- Identifier References – referring to a variable or method that's in scope, simply by its name. (Technically speaking, the call to
DoThis()is both an Identifier References and a Method Call.
These are certainly the most common types of expressions, but Oxygene provides a range of additional expression types that allow you to express more complex values and calculations.
- Member Access expressions allow you to directly access members of a class, record or enum using the
selfkeyword lets you refer to the current class or record to use it in a nested expression, while
inheritedexpressions allow you to explicitly call into the ancestor type.
resultkeyword lets you work with and set the return value of a method.
newexpressions are used to construct new instances of a type by executing its constructor.
Please refer to the nested topics listed in the sidebar on the left for a complete reference of available expression types.
Sophisticated and Advanced Expression Types
Unique to the Oxygene language, the traditional
for statement types can be used as expressions. When used as such,
case Expressions conditionally return different values, while
for Loop Expressions return a whole Sequence of values.
Parenthesis allow you to visually enclose sub-expressions in order to indicate the order of precedence in which they will be executed within a larger expression. This is especially helpful when using Binary Operators, but can also help clarify other more complex expressions.
var x := 3 + 2 * 5 // evaluates to 3 + 10 = 13, as * has precedence over + by default. var x := (3 + 2) * 5 // evaluates to 5 * 5 = 25, as the parenthesis cause the addition to be performed first.
- Address-Of (
- Anonymous Methods
- Arithmetic and Logical Expressions
- Discardables (
- Global Access (
- Lambda Expressions (
- Member Access
- Method Calls
- Parenthesis (
- Pointer Dereference (
- Tuple Literals
- Type Casts (
- Type Checks (