From A Swift Tour — The Swift Programming Language (Swift 5):
var myVariable = 42
myVariable = 50
let myConstant = 42
A constant or variable must have the same type as the value you want to assign to it. However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type. In the example above, the compiler infers that myVariable is an integer because its initial value is an integer.
How does this work?
M+N
the very presence of+
forces bothM
andN
to be of typeInteger
. Now apply this recursively to the whole program. Details are much more complicated, and explained in great detail in the wonderful textbook Types and Programming Languages, one of the best textbooks in all of computer science. (This is probably not the answer you want.) $\endgroup$