UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 10: Structs Part 1 - MUTATING FUNC versus VAR

Forums > 100 Days of SwiftUI

I can't discern the difference between using a mutating function, which Paul makes use of here, and dynamic property values, which he applies here. They seem to do the same thing in a simple setting, so the difference must be for more complicated code.

For example, I can get two identical results from the following (borrowing from some of Paul's code on the 10.2 review/test):

struct Candle {
    var burnLength: Int
    var alreadyBurned = 4
    mutating func burnRemaining() -> Int {
        return burnLength - alreadyBurned
    }
    var burnRemainingVar: Int {
        return burnLength - alreadyBurned
    }
}
var myCandle = Candle(burnLength: 5, alreadyBurned: 2)
myCandle.burnRemaining()
myCandle.burnRemainingVar

Both .burnRemaining() and .burnRemainingVar produce the same value (3, obviously). Aside from the function code requiring a few more typed characters, why would I want to use one 'method' (pun not intended) rather than the other?

2      

Welcome to the Forums.

Here we either (1) Tell You the Answer or (2) Teach You More Than You Want To Know.

let typeOfAnswer = Int.random(in: 1...2)
if typeOfAnswer.isMultiple(of: 2) {
    print("Today you get lots of detail.")
} else {
    print("Lucky you. Today just the answer.")
}

Today you get lots of detail

Mutating mean changing.

In your examples, you aren't changing, you're just using. So sure, both your examples work just fine. Paste this code into Playgrounds and remove the mutating key word. How does the compiler respond?

// Paste into Playgrounds.
struct Candle {
    var burnLength: Int
    var alreadyBurned = 4

    var burnRemainingVar: Int {
        // This is NOT changing anything.
        burnLength - alreadyBurned  // by the way, return isn't necessary here.
    }
    func burnRemaining() -> Int {
        // Also, NOT changing anything.
        return burnRemainingVar // just pass the var back.
    }
    // Try this without the mutating keyword. Compiler will complain.
     mutating func burnRemaining(thisLong: Int) -> Int {
        alreadyBurned += thisLong // MUTATE the var
        return burnRemaining()  // return IS required here. Do you know why?
    }
}

// Test cases
var myCandle = Candle(burnLength: 5, alreadyBurned: 2)
myCandle.burnRemainingVar  // print the var value
myCandle.burnRemaining()   // execute a NON mutating function
myCandle.burnRemaining(thisLong: 2) // execute a MUTATING function

Keep asking questions!

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.