One would expect both of these minimalistic programs to be semantically equivalent and thus be handled identically by the compiler, no?
The answer to this question and many more can be found in the Swift project’s official documentation on its “Type Checker Design and Implementation” (emphasis mine):
Swift implements bi-directional type inference using a constraint-based type checker that is reminiscent of the classical Hindley-Milner type inference algorithm. […]
The Swift language contains a number of features not part of the Hindley-Milner type system, including constrained polymorphic types and function overloading, which complicate the presentation and implementation somewhat. On the other hand, Swift limits the scope of type inference to a single expression or statement, for purely practical reasons: we expect that we can provide better performance and vastly better diagnostics when the problem is limited in scope.
Repeat after me: “Swift limits the scope of type inference to a single expression or statement”.
Stay up-to-date by subscribing to the Comments RSS Feed for this post.