Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
let points = 127
let level = Int8(points)
print(level + 1)The program crashes
- Expecting
-128: only&+wraps around like that. The plain+checks the result and crashes the program when the result does not fit the type. - Expecting
127: Swift never clamps a result to the type's limit. A sum that does not fit inInt8stops the program instead.
- What does
print(level &+ 1)print instead? - Which type would you choose for
levelso that adding 1 here never crashes?