Reputation: 36013
Is there any gain in speed, memory usage, whatever, in Swift by defining as much as possible constants x vars?
I mean, defining as much as possible with let
instead of var
?
Upvotes: 7
Views: 792
Reputation: 130210
In theory, there should be no difference in speed or memory usage - internally, the variables work the same. In practice, letting the compiler know that something is a constant might result in better optimisations.
However the most important reason is that using constants (or immutable objects) helps to prevent programmer errors. It's not by accident that method parameters and iterators are constant by default.
Using immutable objects is also very useful in multithreaded applications because they prevent one type of synchronization problems.
Upvotes: 12