Daily iOS
Swift

Type inference cost

Swift has type inference, so you rarely have to write types, yet your team convention says "annotate types on complex expressions and public APIs." How does type inference work that led to this rule?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Swift's type inference is bidirectional: it gathers constraints across the whole expression and solves them together, so in a long expression full of literals and overloaded operators the candidate combinations explode, compilation slows down, or you get an 'expression too complex' error. Writing the type prunes the candidates so solving is faster, and it makes the API contract visible.

Share a question