What is type inference?
· One min read
Sources & Resources
TL/DR
Type inference is the mechanism that enables Swift to infer the type of a variable without us having to specify it explicitly. This generally lends itself to writing cleaner and more concise code without compromising readability or type safety.
That’s why we can write this:
var welcomeMessage = "Hello"
Instead of having to write (a.k.a type annotations):
var welcomeMessage: String = "Hello"
The compiler is able to infer that welcomeMessage
is a String
based off of the default value we’ve provided.
If we don’t specify a default value, then we’ll need to use type annotation to provide the compiler with the relevant information:
var red, green, blue: Double