0
0
Swiftprogramming~10 mins

Type inference by the compiler in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type inference by the compiler
Start
Declare variable with value
Compiler checks value type
Compiler assigns variable type
Use variable with inferred type
End
The compiler looks at the value assigned to a variable and automatically decides the variable's type without needing explicit type annotation.
Execution Sample
Swift
let number = 42
let message = "Hello"
let pi = 3.14
This code declares three constants with values, and the compiler infers their types as Int, String, and Double respectively.
Execution Table
StepCode LineActionInferred TypeVariable Value
1let number = 42Assign 42 to numberInt42
2let message = "Hello"Assign "Hello" to messageString"Hello"
3let pi = 3.14Assign 3.14 to piDouble3.14
4EndAll variables declared with inferred types--
💡 All variables have been assigned values, so the compiler infers their types and finishes.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
numberuninitialized42 (Int)42 (Int)42 (Int)42 (Int)
messageuninitializeduninitialized"Hello" (String)"Hello" (String)"Hello" (String)
piuninitializeduninitializeduninitialized3.14 (Double)3.14 (Double)
Key Moments - 3 Insights
Why don't we need to write the type explicitly like 'let number: Int = 42'?
Because the compiler looks at the value '42' and automatically knows 'number' should be an Int, as shown in execution_table step 1.
What happens if we assign a value of a different type later to the same variable?
In Swift, variables declared with 'let' are constants and cannot change. Also, the compiler fixes the type at declaration, so assigning a different type later would cause an error.
How does the compiler know that 3.14 is a Double and not a Float?
By default, Swift treats decimal numbers like 3.14 as Double, so the compiler infers 'pi' as Double in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What type does the compiler infer for 'message'?
ADouble
BInt
CString
DBool
💡 Hint
Check the 'Inferred Type' column for step 2 in the execution_table.
At which step does the compiler infer the type Double?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Inferred Type' column and find where 'Double' appears.
If we change 'let number = 42' to 'let number = 42.0', what type will the compiler infer?
AInt
BDouble
CString
DFloat
💡 Hint
Decimal numbers like 42.0 are inferred as Double by default in Swift.
Concept Snapshot
Type inference lets the compiler guess variable types from assigned values.
Syntax: let variable = value
No need to write type explicitly.
Compiler assigns type like Int, String, Double automatically.
Helps write cleaner and shorter code.
Full Transcript
In Swift, when you declare a variable or constant and assign it a value, the compiler automatically figures out the type of that variable. For example, if you write 'let number = 42', the compiler sees the number 42 and knows 'number' is an Int. This is called type inference. It means you don't have to write the type yourself. The compiler uses the value to decide the type. This works for strings, numbers with decimals, and more. The compiler fixes the type when you declare the variable, so you cannot later assign a different type to it. This feature helps you write code faster and cleaner without losing type safety.