Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift, optional unwrapping, reversing if condition

Let's say I have function which returns optional. nil if error and value if success:

func foo() -> Bar? { ... }

I can use following code to work with this function:

let fooResultOpt = foo()
if let fooResult = fooResultOpt {
    // continue correct operations here
} else {
    // handle error
}

However there are few problems with this approach for any non-trivial code:

  1. Error handling performed in the end and it's easy to miss something. It's much better, when error handling code follows function call.

  2. Correct operations code is indented by one level. If we have another function to call, we have to indent one more time.

With C one usually could write something like this:

Bar *fooResult = foo();
if (fooResult == null) {
    // handle error and return
}
// continue correct operations here

I found two ways to achieve similar code style with Swift, but I don't like either.

let fooResultOpt = foo()
if fooResult == nil {
    // handle error and return
}
// use fooResultOpt! from here
let fooResult = fooResultOpt! // or define another variable

If I'll write "!" everywhere, it just looks bad for my taste. I could introduce another variable, but that doesn't look good either. Ideally I would like to see the following:

if !let fooResult = foo() {
    // handle error and return
}
// fooResult has Bar type and can be used in the top level

Did I miss something in the specification or is there some another way to write good looking Swift code?

like image 503
vbezhenar Avatar asked Oct 10 '14 17:10

vbezhenar


4 Answers

Your assumptions are correct—there isn't a "negated if-let" syntax in Swift.

I suspect one reason for that might be grammar integrity. Throughout Swift (and commonly in other C-inspired languages), if you have a statement that can bind local symbols (i.e. name new variables and give them values) and that can have a block body (e.g. if, while, for), those bindings are scoped to said block. Letting a block statement bind symbols to its enclosing scope instead would be inconsistent.

It's still a reasonable thing to think about, though — I'd recommend filing a bug and seeing what Apple does about it.

like image 136
rickster Avatar answered Oct 11 '22 15:10

rickster


This is what pattern matching is all about, and is the tool meant for this job:

let x: String? = "Yes"

switch x {
case .Some(let value):
  println("I have a value: \(value)")
case .None:
  println("I'm empty")
}

The if-let form is just a convenience for when you don't need both legs.

like image 26
Rob Napier Avatar answered Oct 11 '22 15:10

Rob Napier


If what you are writing is a set of functions performing the same sequence of transformation, such as when processing a result returned by a REST call (check for response not nil, check status, check for app/server error, parse response, etc.), what I would do is create a pipeline that at each steps transforms the input data, and at the end returns either nil or a transformed result of a certain type.

I chose the >>> custom operator, that visually indicates the data flow, but of course feel free to choose your own:

infix operator >>> { associativity left }

func >>> <T, V> (params: T?, next: T -> V?) -> V? {
    if let params = params {
        return next(params)
    }
    return nil
}

The operator is a function that receives as input a value of a certain type, and a closure that transforms the value into a value of another type. If the value is not nil, the function invokes the closure, passing the value, and returns its return value. If the value is nil, then the operator returns nil.

An example is probably needed, so let's suppose I have an array of integers, and I want to perform the following operations in sequence:

  • sum all elements of the array
  • calculate the power of 2
  • divide by 5 and return the integer part and the remainder
  • sum the above 2 numbers together

These are the 4 functions:

func sumArray(array: [Int]?) -> Int? {
    if let array = array {
        return array.reduce(0, combine: +)
    }

    return nil
}

func powerOf2(num: Int?) -> Int? {
    if let num = num {
        return num * num
    }
    return nil
}

func module5(num: Int?) -> (Int, Int)? {
    if let num = num {
        return (num / 5, num % 5)
    }
    return nil
}

func sum(params: (num1: Int, num2: Int)?) -> Int? {
    if let params = params {
        return params.num1 + params.num2
    }
    return nil
}

and this is how I would use:

let res: Int? = [1, 2, 3] >>> sumArray >>> powerOf2 >>> module5 >>> sum

The result of this expression is either nil or a value of the type as defined in the last function of the pipeline, which in the above example is an Int.

If you need to do better error handling, you can define an enum like this:

enum Result<T> {
    case Value(T)
    case Error(MyErrorType)
}

and replace all optionals in the above functions with Result<T>, returning Result.Error() instead of nil.

like image 1
Antonio Avatar answered Oct 11 '22 17:10

Antonio


I've found a way that looks better than alternatives, but it uses language features in unrecommended way.

Example using code from the question:

let fooResult: Bar! = foo();
if fooResult == nil {
    // handle error and return
}
// continue correct operations here

fooResult might be used as normal variable and it's not needed to use "?" or "!" suffixes.

Apple documentation says:

Implicitly unwrapped optionals are useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter. The primary use of implicitly unwrapped optionals in Swift is during class initialization, as described in Unowned References and Implicitly Unwrapped Optional Properties.

like image 1
vbezhenar Avatar answered Oct 11 '22 17:10

vbezhenar