Expressions
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 "var
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.
For example, the condition within an if
/then
is an expression (of Boolean type). If you declare a variable with the var
Statement and assign it an initial value, that value is an 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
*
or<
. - 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.
For example:
- Member Access expressions allow you to directly access members of a class, record or enum using the
.
or:
operator. - The
self
keyword lets you refer to the current class or record to use it in a nested expression, whileinherited
expressions allow you to explicitly call into the ancestor type. - The
result
keyword lets you work with and set the return value of a method. new
expressions 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 if
, case
and for
statement types can be used as expressions. When used as such, if
and case
Expressions conditionally return different values, while for
Loop Expressions return a whole Sequence of values.
async
expressions can take any given Statement (not just expressions), and turn it into a Future that will be executed asynchronously in the background.
from
expressions allow you to use LINQ to write strongly typed code that can query, filter and otherwise work with collections and Sequences of objects.
Parenthesis
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.
All Expressions
- Address-Of (
@
) - Anonymous Methods
- Arithmetic and Logical Expressions
- ArrayLiterals
async
Expressionsawait
Expressionscase
Expressionsconstructor
Calls- Discardables (
nil
) for
Loop Expressionsfrom
LINQ ExpressionsIdentifier
- Global Access (
:
) if
/then
/else
Expressionsimplies
Operatorin
Operatoer- Indexers
inherited
Operator- Lambda Expressions (
->
) - Literals
locking
Expressionsmapped
- Member Access
- Method Calls
new
Expressions- Operators
- Parenthesis (
()
) - Pointer Dereference (
^
) raise
Expressionsresult
selector()
self
- Tuple Literals
- Type Casts (
as
) - Type Checks (
is
)