Introduction
Swift uses type inference to automatically determine the type of a constant or variable from the value assigned to it, so you rarely need to write explicit type annotations. However, once a type is set, Swift never silently converts a value from one type to another; any conversion between types, such as from Int to Double or from Int to String, must be done explicitly through an initializer.
Cricket analogy: Swift inferring a constant's type from its value is like a scorer automatically recording '4' as a boundary the moment it's hit, without you specifying the shot type, but converting overs to balls still requires an explicit calculation, never assumed silently.
Syntax
let x = 5 // inferred as Int
let y: Double = 5.0 // explicit type
let z = Double(x) // explicit conversion from Int to Double
let s = String(x) // explicit conversion from Int to StringExplanation
When you write let x = 5, Swift examines the literal 5 and infers its type as Int because that is the default type for integer literals. If you write let x: Double = 5.0, you are giving Swift an explicit type annotation, which is only needed when you want a type other than what would be inferred, or when there is no initial value to infer from. Because Swift never performs implicit conversions, combining values of different types in an expression requires you to convert one of them explicitly, typically using an initializer like Double(someInt) or String(someInt). This explicitness is a deliberate design choice that prevents silent data loss and makes every conversion visible in the code.
Cricket analogy: Writing let x = 5 and letting Swift infer Int is like a scorer defaulting a tally mark to a single run unless told otherwise, while combining a run count with a strike rate percentage still requires an explicit conversion, like Double(runs).
Example
let count = 7 // inferred Int
let average: Double = 2.5 // explicit Double
let combined = Double(count) * average
let number = 42
let text = "The answer is " + String(number)
print(combined)
print(text)Output
17.5
The answer is 42Key Takeaways
- Type inference lets Swift determine a value's type from its literal without an explicit annotation.
- Explicit type annotations (name: Type) are needed only when you want a non-default type.
- Swift never implicitly converts between types like Int and Double.
- Conversions must be explicit, using initializers such as Double(someInt) or String(someInt).
- This explicitness avoids silent precision loss and makes conversions easy to spot in code review.
Practice what you learned
1. What type does Swift infer for the declaration let x = 5?
2. How do you explicitly convert an Int variable named count to a Double in Swift?
3. When is an explicit type annotation required in Swift?
4. What happens if you write let x: Double = 5?
Was this page helpful?
You May Also Like
Variables and Constants in Swift
Learn how Swift's var and let keywords declare mutable variables and immutable constants, and why let is preferred by default.
Data Types in Swift
Explore Swift's core data types—Int, Double, Float, Bool, String, Character, and tuples—and how Swift handles numeric type safety.
String Interpolation in Swift
See how Swift's backslash-parenthesis syntax embeds expressions inside string literals, including multi-line triple-quoted strings.
Optionals in Swift
Optionals let a Swift variable represent either a value or the absence of one, forming the basis of Swift's null-safety.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics