1

I'm trying to understand Swift's Enum. I want to assign my enum case an associated value and have a computed value returned. Currently I'm using a method for returning the calculated number.

enum Calculation {
    case Number1(number: Int)
    case Number2(number: Int)

    func calculation() -> Int {
        switch self {
        case Number1(let number): return number + 1
        case Number2(let number): return number + 2
        }
    }
}

I was wondering if it could be done shorter like:

// This obviously doesn't work. Just an example as what I'm trying to achieve
enum Calculation: Int {
    case Number1(number: Int): return number + 1
    case Number2(number: Int): return number + 2
}
Laap
  • 71
  • 1
  • 7
  • 1
    This answer may help you. http://stackoverflow.com/a/32530320/3222419 – David Williames Apr 05 '16 at 11:55
  • 1
    Possible duplicate of [Swift: Enums that use closures?](http://stackoverflow.com/questions/31450971/swift-enums-that-use-closures) – bufh Apr 05 '16 at 12:05
  • `"I was wondering if it could be done shorter like: example"` Did you even bother trying and finding out that your example doesn't work? Did you put any research effort in whatsoever? Your syntax doesn't even really make sense to me... – nhgrif Apr 05 '16 at 12:20
  • @DavidWilliames I can't see what the linked answer has anything to do with this really. There's nothing about closures here... did you just link the first enum associated type question you found? – nhgrif Apr 05 '16 at 12:22
  • @nhgrif- No I did not simply link the first enum related question I could find!! It appears that what he is trying to do is store a 'calculation' as an enum that essentially performs a calculation on a number and returns it's result.... that is essentially wanting to store closures in his enum. – David Williames Apr 05 '16 at 12:26
  • I'm still looking at the suggested answer, but it doesn't seem it's what I want though. @nhgrif Yes I have. With that last example, I'm very well aware that it doesn't work. I was only hoping to make it easier and clear to the readers as what I want as a result. – Laap Apr 05 '16 at 12:36
  • @DavidWilliames That moves control over the actual calculation from *inside* this type to *outside* this type, and even still, it requires actually performing the calculation... and that block of code probably isn't *really* that much simpler than his current `calculation()` method implementation. – nhgrif Apr 05 '16 at 12:43

1 Answers1

2

The question is whether you want to use a computed variable:

enum Calculation {
    case Number1(number: Int)
    case Number2(number: Int)

    var value:Int {
        switch self {
        case Number1(let number): return number + 1
        case Number2(let number): return number + 2
        }
    }

}

Or have the value stored as the associated value:

enum CalculationType {
    case Number1, Number2
}
enum Calculation {

    case Number1(number: Int)
    case Number2(number: Int)

    init (type:CalculationType, number:Int) {
        switch type {
        case .Number1:
            self = Calculation.Number1(number: number + 1)
        case .Number2:
            self = Calculation.Number2(number: number + 2)
        }

    }
}

Your question points towards the former but I'm not clear from your second piece of code whether you were expecting the latter or hoping that initialization would return a value, which can't happen (an init does not return a value). The closest thing would be to have a static function:

enum CalculationType {
    case Number1, Number2
}
enum Calculation {
    case Number1(number: Int)
    case Number2(number: Int)

    static func calc(type:CalculationType, number:Int) -> (Calculation, Int) {
        switch type {
        case .Number1:
            return (Calculation.Number1(number: number), number + 1)
        case .Number2:
            return  (Calculation.Number2(number: number), number + 2)
        }
    }

}

Calculation.calc(.Number1, number: 2)

Alternatively you could use a callback on the init:

enum CalculationType {
    case Number1, Number2
}
enum Calculation {

    case Number1(number: Int)
    case Number2(number: Int)

    init (type:CalculationType, number:Int, callback:(Int) -> Void) {
        switch type {
        case .Number1:
            self = Calculation.Number1(number: number)
            callback(number + 1)
        case .Number2:
            self = Calculation.Number2(number: number)
            callback(number + 2)
        }

    }

}


Calculation(type: .Number1, number: 10, callback: {n in print(n)})
sketchyTech
  • 5,746
  • 1
  • 33
  • 56