Expressions Flashcards
What kinds of expressions are there in Swift?
In Swift, there are four kinds of expressions: prefix expressions, binary expressions, primary expressions, and postfix expressions.
Prefix and binary expressions let you apply operators to smaller expressions.
Primary expressions are conceptually the simplest kind of expression, and they provide a way to access values.
Postfix expressions, like prefix and binary expressions, let you build up more complex expressions using postfixes such as function calls and member access.
What does evaluating an expression do?
Evaluating an expression returns a value, causes a side effect, or both.
Name the prefix operators defined by the Swift standard library.
\++ Increment -- Decrement ! Logical NOT ~ Bitwise NOT \+ Unary plus - Unary minus
What other prefix operator is defined by the Swift language?
In addition to the standard library operators listed above, you use & immediately before the name of a variable that’s being passed as an in-out argument to a function call expression.
What exponentiative binary operators does the Swift standard library provide?
«_space;Bitwise left shift
» Bitwise right shift
(No associativity, precedence level 160)
What multiplicative binary operators does the Swift standard library provide?
* Multiply / Divide % Remainder &* Multiply, ignoring overflow &/ Divide, ignoring overflow &% Remainder, ignoring overflow & Bitwise AND
(Left associative, precedence level 150)
What additive binary operators does the Swift standard library provide?
\+ Add - Subtract &+ Add with overflow &- Subtract with overflow | Bitwise OR ^ Bitwise XOR
(Left associative, precedence level 140)
What range binary operators does the Swift standard library provide?
.. Half-closed range
… Closed range
(No associativity, precedence level 135)
What cast binary operators does the Swift standard library provide?
is Type check
as Type cast
(No associativity, precedence level 132)
What comparative binary operators does the Swift standard library provide?
< Less than Greater than >= Greater than or equal == Equal != Not equal === Identical !== Not identical ~= Pattern match
(No associativity, precedence level 130)
What conjunctive binary operator does the Swift standard library provide?
&& Logical AND
Left associative, precedence level 120
What disjunctive binary operator does the Swift standard library provide?
|| Logical OR
Left associative, precedence level 110
What assignment binary operators does the Swift standard library provide?
= Assign *= Multiply and assign /= Divide and assign %= Remainder and assign \+= Add and assign -= Subtract and assign <>= Right bit shift and assign &= Bitwise AND and assign ^= Bitwise XOR and assign |= Bitwise OR and assign &&= Logical AND and assign ||= Logical OR and assign
(Right associative, precedence level 90)
What ternary operator does the Swift standard library provide?
?: Ternary conditional
Right associative, precedence level 100
How are binary operator precedence rules applied?
At parse time, an expression made up of binary operators is represented as a flat list. This list is transformed into a tree by applying operator precedence
For example, the expression 2 + 3 * 5 is initially understood as a flat list of five items, 2, +, 3, *, and 5.
This process transforms it into the tree (2 + (3 * 5)).
Which binary operator is this?
~=
Pattern match
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
||
Logical OR
Category: Disjunctive
Left associative
Precedence level 110
Which binary operator is this?
*=
Multiply and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
&-
Subtract with overflow
Category: Additive
Left associative
Precedence level 140
Which binary operator is this?
> =
Greater than or equal
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
&=
Bitwise AND and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
/=
Divide and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
^=
Bitwise XOR and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
-=
Subtract and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
==
Equal
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
&*
Multiply, ignoring overflow
Category: Multiplicative
Left associative
Precedence level 150
Which binary operator is this?
!==
Not identical
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
is
Type check
Category: Cast
No associativity
Precedence level 132
Which binary operator is this?
> >
Bitwise right shift
Category: Exponentiative
No associativity
Precedence level 160
Which binary operator is this?
..
Half-closed range
Category: Range
No associativity
Precedence level 135
Which binary operator is this?
%
Remainder
Category: Multiplicative
Left associative
Precedence level 150
Which binary operator is this?
!=
Not Equal
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
*
Multiply
Category: Multiplicative
Left associative
Precedence level 150
Which binary operator is this?
> > =
Right bit shift and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
«=
Left bit shift and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
-
Subtract
Category: Additive
Left associative
Precedence level 140
Which binary operator is this?
/
Divide
Category: Multiplicative
Left associative
Precedence level 150
Which binary operator is this?
Less than
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
+
Add
Category: Additive
Left associative
Precedence level 140
Which binary operator is this?
===
Identical
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
Bitwise XOR
Category: Additive
Left associative
Precedence level 140
Which binary operator is this?
<=
Less than or equal
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
&&=
Logical AND and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
&+
Add with overflow
Category: Additive
Left associative
Precedence level 140
Which binary operator is this?
%=
Remainder and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
|=
Bitwise OR and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
&/
Divide, ignoring overflow
Category: Multiplicative
Left associative
Precedence level 150
Which binary operator is this?
&
Bitwise AND
Category: Multiplicative
Left associative
Precedence level 150
Which binary operator is this?
…
Closed range
Category: Range
No associativity
Precedence level 135
Which binary operator is this?
<
Bitwise left shift
Category: Exponentiative
No associativity
Precedence level 160
Which binary operator is this?
>
Greater than
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
|
Bitwise OR
Category: Additive
Left associative
Precedence level 140
Which binary operator is this?
&%
Remainder, ignoring overflow
Category: Multiplicative
Left associative
Precedence level 150
Which binary operator is this?
=
Assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
&&
Logical AND
Category: Conjunctive
Left associative
Precedence level 120
Which binary operator is this?
as
Type cast
Category: Cast
No associativity
Precedence level 132
Which binary operator is this?
||=
Logical OR and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
+=
Add and assign
Category: Assignment
Right associative
Precedence level 90
What are the rules for the assignment operator?
=
The assigment operator sets a new value for a given expression. It has the following form:
expression = value
The value of the expression is set to the value obtained by evaluating the value. If the expression is a tuple, the value must be a tuple with the same number of elements. (Nested tuples are allowed.) Assignment is performed from each part of the value to the corresponding part of the expression. For example:
(a, _, (b, c)) = ("test", 9.45, (12, 3)) //a is "test", b is 12, c is 3, and 9.45 is ignored
The assignment operator does not return any value.
What are the rules for the ternary operator?
? :
The ternary conditional operator evaluates to one of two given values based on the value of a condition. It has the following form:
condition ? a : b
If the condition evaluates to true, the conditional operator evaluates the first expression and returns its value. Otherwise, it evaluates the second expression and returns its value. The unused expression is not evaluated.
What are the rules for the typecasting operator?
as
If conversion to the specified type is guaranteed to succeed, the value of the expression is returned as an instance of the specified type.
If conversion to the specified type is guaranteed to fail, a compile-time error is raised.
Otherwise, if it’s not known at compile time whether the conversion will succeed, the type of the cast expresion is an optional of the specified type. At runtime, if the cast succeeds, the value of expression is wrapped in an optional and returned; otherwise, the value returned is nil.
What are the rules for the type-testing operator?
is
The is operator checks at runtime to see whether the expression is of the specified type. If so, it returns true; otherwise, it returns false.
The check must not be known to be true or false at compile time.