<commit_before># Mutability of Values types

- **Collections** in Swift are `value` type if the **containing** elements are `value` types
- **Collections** in Swift are `reference` type if the **containing** elements are `reference` types


## Collections of value types

- Immutable Collections containing value types: [declared as `let`]
  - value types with mutating behaviour      -> _can NOT change any element in the Collection_
  - value types with no mutating behaviour   -> _can NOT change any element in the Collection_

- Mutable Collections containing value types: [declared as `var`]
  - value types with mutating behaviour      -> _**CAN** change any element in the Collection_
  - value types with no mutating behaviour   -> _can NOT change any element in the Collection_

## Inout
Parameters with `inout` prefix, is similar to mutating of self in a Struct. I.e the parameter is first copied, then mutated and finally returned back to the call-site. The parameter is only mutated inside the function scope, thus it will not have any side-effect outside of the function is specified in. See example below.


## Example

```swift
import Foundation

struct ValueAccount {
    var balance: Int
    
    func depositing(amount: Int) -> ValueAccount {
        var copy = self
        copy.deposit(amount: amount)<commit_msg>Update Struct+Mutability.md<commit_after># Mutability of Values types

- **Collections** in Swift are `reference` type if the **containing** elements are `reference` types
- **Collections** in Swift are `value` type if the **containing** elements are `value` types, even if the value types can be **mutated**


## Collections of value types

- Immutable Collections containing value types: [declared as `let`]
  - value types with mutating behaviour      -> _can NOT change any element in the Collection_
  - value types with no mutating behaviour   -> _can NOT change any element in the Collection_

- Mutable Collections containing value types: [declared as `var`]
  - value types with mutating behaviour      -> _**CAN** change any element in the Collection_
  - value types with no mutating behaviour   -> _can NOT change any element in the Collection_

## Inout
Parameters with `inout` prefix, is similar to mutating of self in a Struct. I.e the parameter is first copied, then mutated and finally returned back to the call-site. The parameter is only mutated inside the function scope, thus it will not have any side-effect outside of the function is specified in. See example below.


## Example

```swift
import Foundation

struct ValueAccount {
    var balance: Int
    
    func depositing(amount: Int) -> ValueAccount {
        var copy = self
        copy.deposit(amount: amount)
{'fraction_non_alphanumeric': 0.07011747430249633, 'fraction_numerical': 0.0, 'mean_word_length': 3.8230088495575223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6064256', 'n_tokens_mistral': 691, 'n_tokens_neox': 665, 'n_words': 362}