The Freya Programming Language

Conditional expressions

See also

Freya allows conditional expressions with a simple and intuitive syntax that imitates conditional statements. These expressions are equivalent to C#'s expressions based in the ternary infix operator x ? y : z.

Syntax

Conditional expressions have this syntax:

if condition then expression1 else expression2

The condition must evaluate as a boolean expression. The result type of the whole conditional is the common type of expression1 and expression2.

Conditional expressions are very useful for inline implementations like this:

method Factorial(I: Integer): LongInt =>
	if I <= 1 then 1 else I * Factorial(I - 1);

Conditional expressions also come handy for initializing inline variable declarations:

var rate :=
    if DateTime.Today.DayOfWeek in [DayOfWeek.Saturday, DayOfWeek.Sunday]
    then 120.0 else 100.0;

Without conditionals, we would be forced to write something like this:

var rate: Double;
begin
    // ...
    if DateTime.Today.DayOfWeek in [DayOfWeek.Saturday, DayOfWeek.Sunday] then
        rate := 120
    else
        rate := 100;
    // ...
end;

loosing all the advantages of an inline declaration.

See also

The Freya Programming Language
Expressions
Common expression blocks
The null coalescing operator
if statement