Difference between revisions of "W1037 Expressions and Operators"

From Coder Merlin
Line 8: Line 8:
== Introduction ==
== Introduction ==
An '''Expression''' is a finite series of symbols that is well-formed according to agreed upon rules.  The symbols may include '''constants''', '''variables''', '''operators''', and '''brackets'''.  A valid expression will yield a value of a specific type according to the rules of the language.
An '''Expression''' is a finite series of symbols that is well-formed according to agreed upon rules.  The symbols may include '''constants''', '''variables''', '''operators''', and '''brackets'''.  A valid expression will yield a value of a specific type according to the rules of the language.
== Type Inference ==
All expressions, when evaluated, yield a particular '''type'''.  For example, in the expression:
<syntaxhighlight lang="swift">
let x = 3
</syntaxhighlight>
''x'' will be of type '''Int''', which is an integer whose size depends on the size of the underlying platform.  (On 32-bit platforms, Int will be equivalent to '''Int32''', and on 64-bit platforms, Int will be equivalent to '''Int64'''.)  On the other hand, in the expression:
<syntaxhighlight lang="swift">
let y = 3.0
</syntaxhighlight>
''y'' will be of type '''Double''', which is a double-precision, floating-point type.  '''Because Swift is a type-safe language, all expressions will always have a ''specific type'''''.  Type-safety enables the compiler to warn us when we make a mistake, such as by using an Int when we intended a Double.
Like many other type-safe languages, we're able to ''explicitly'' specify the type of a constant of variable:
<syntaxhighlight lang="swift">
let x : Int = 3
</syntaxhighlight>
We're able to use explicit typing to ensure that we're evaluating the expression as the desired type as in the example above. However, in most cases, Swift allows us to be more brief, and rely on '''type inference'''.  Type inference will rely on the literal value(s) or named value(s) to determine the type.  Let's look at a few examples:
<syntaxhighlight lang="swift">
let maximumClassCount = 8
// maximumClassCount will be of type Int
let maximumGPA = 5.0
// maximumGPA will be of type Double
</syntaxhighlight>


== Topic Headers ==
== Topic Headers ==

Revision as of 19:22, 28 October 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
ComingSoonIcon.png
Coming Soon
Expressions
The expression of the emotions in man and animals (1872)

Prerequisites[edit]

Introduction[edit]

An Expression is a finite series of symbols that is well-formed according to agreed upon rules. The symbols may include constants, variables, operators, and brackets. A valid expression will yield a value of a specific type according to the rules of the language.

Type Inference[edit]

All expressions, when evaluated, yield a particular type. For example, in the expression:

let x = 3

x will be of type Int, which is an integer whose size depends on the size of the underlying platform. (On 32-bit platforms, Int will be equivalent to Int32, and on 64-bit platforms, Int will be equivalent to Int64.) On the other hand, in the expression:

let y = 3.0

y will be of type Double, which is a double-precision, floating-point type. Because Swift is a type-safe language, all expressions will always have a specific type. Type-safety enables the compiler to warn us when we make a mistake, such as by using an Int when we intended a Double. Like many other type-safe languages, we're able to explicitly specify the type of a constant of variable:

let x : Int = 3

We're able to use explicit typing to ensure that we're evaluating the expression as the desired type as in the example above. However, in most cases, Swift allows us to be more brief, and rely on type inference. Type inference will rely on the literal value(s) or named value(s) to determine the type. Let's look at a few examples:

let maximumClassCount = 8
// maximumClassCount will be of type Int

let maximumGPA = 5.0
// maximumGPA will be of type Double

Topic Headers[edit]

Key Concepts[edit]

Exercises[edit]

References[edit]