Introduction
In this blog, we will go through how to read the user's standard Swift Expression, Statements and Comments.
In this blog, we'll master some programming fundamentals while using the Swift programming language in a modern, user-friendly environment.
Instead of boring you with theory, you'll get right to coding with Swift Playgrounds, which are sandbox-style environments where you can run code without building a complete programme.
You'll need Xcode 10, the standard development environment for macOS, which you can get here. Some of the code in this lesson will not work if you use Xcode version 9 or lower.
The toolchain refers to the collection of tools you employ to build software. The Integrated Development Environment is the toolchain component where you create your code (IDE). Xcode is your IDE, and it comes with playgrounds.
You'll use playgrounds to practise coding throughout this series, so it's crucial to know how they work. That is exactly what the remainder of this tutorial will teach you.
Expressions
Swift's four types of expressions are prefix expressions, infix expressions, primary expressions, and postfix expressions. You get a value, a side effect, or both when you evaluate a word.
Operators can be applied to smaller expressions using prefix and infix expressions. Primary expressions are the simplest type of expression in terms of notion, and they allow you to access values. Postfix expressions, like prefix and infix expressions, will enable you to combine postfixes like function calls and member access to create more sophisticated expressions. The sections following go over each type of expression in detail.
Variables, operators, literals, and functions are all part of an expression. For instance,
var marks = 80; / assign value to marks
var result = (num1 == num2); / compare num1 and num2
Expressions are as follows:
num1 == num2 - compares num1 and num2 var marks = 80 - indicates that we are assigning 80 to marks
The sections following go over some types of expression in detail.
Prefix Expressions
Prefix expressions combine an expression with an optional prefix operator. The expression that follows the prefix operator has only one argument.
See Basic Operators and Advanced Operators for further information on these operators' behaviour.
See Operator Declarations for further information on the Swift standard library's operators.
In-Out Expression
An in-out expression designates a variable supplied as an in-out argument to a function call expression.
&expression
See In-Out Parameters for further information and an example of in-out parameters.
As mentioned in Implicit Conversion to a Pointer Type, in-out expressions are also utilised when delivering a non-pointer argument in a situation where a pointer is required.
Infix Expressions
Infix expressions combine an infix binary operator with the left- and right-hand arguments. It is written as follows:
right-hand argument operator, left-hand argument operator
See Basic Operators and Advanced Operators for further information on these operators' behaviour.
See Operator Declarations for further information on the Swift standard library's operators.
NOTE
An expression of infix operators is represented as a flat list at parse time. The operator precedence is used to turn this list into a tree. The statement 2 + 3 * 5 is, for example, interpreted initially as a flat list of five items, 2, +, 3, *, and 5. It becomes the tree (2 + (3 * 5) due to this process.
Primary Expressions
The most fundamental type of expression is primary expression. They can be used alone as expressions or coupled with other tokens to form prefix, infix, and postfix expressions.
A PRIMARY EXPRESSION'S GRAMMAR
generic-argument-clause opt primary-expression identifier
literal-expression primary-expression
self-expression primary-expression
superclass-expression primary-expression
closure-expression vs primary-expression
parenthesized-expression primary-expression
tuple-expression primary-expression
implicit-member-expression primary-expression
wildcard-expression primary-expression
key-path-expression primary-expression
selector-expression vs primary-expression
key-path-string-expression primary-expression
Literal Expression
A conventional literal (such as a string or an integer), an array or dictionary literal, a playground literal, or one of the special literals listed below make up a literal expression.
Literal Type Value
#file String
The file's path.
#fileID String
Its file and module names.
#filePath String
The file's path.
It appears online #line Int.
It starts in column #column Int.
#function String
Its declaration's name.
#dsohandle UnsafeRawPointer
Where it appears, the handle of a dynamic shared object (DSO).
To facilitate migration from the previous #filePath behaviour to the new #fileID behaviour, the string value of #file depends on the language version. #file currently has the same value as #filePath. #file will have the same value as #fileID in a future version of Swift. To switch to the new behaviour, replace #file with #fileID or #filePath as needed.
A #fileID expression's string value is of the form module/file, where the file is the name of the file in which the word appears, and a module is the name of the module in which this file is included. A #filePath expression's string value is the whole file-system path to the file where the word appears.