From the course: Learning Functional Programming with Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Immutability and avoiding state change

Immutability and avoiding state change - Swift Tutorial

From the course: Learning Functional Programming with Swift

Start my 1-month free trial

Immutability and avoiding state change

- [Instructor] The first major concept of functional programming is one that may surprise a lot of people. This is the concept of immutability. Most programmers learned early on that you could assign a value to a variable then later on change the value of that variable. However, in functional programming this generally isn't allowed. In fact, many functional programming languages such as Haskell and LISP prohibit it completely. But in most real-world applications we settle for simply keeping it to a minimum. In short, immutability means that most of the values in a program are constant, which in Swift means using the let key word instead instead of the var key word. Another way to think about it is that in object-oriented languages we assign values to variables. A variable, say X, can be thought of as a container that we can put different values into. At one point in time X might hold the value of three, at another point it might hold the value of 10, at another point it might be…

Contents