Returning an implicitly unwrapped optional

Jumhyn

I'm writing a program in Swift which has various functions which should always return values, but in some cases cannot, and so the program crashes and alerts the user of the error. To accomplish this, I am using implicitly unwrapped Optionals as return values for my functions, so that error cases can just return nil (even though that code will never be reached). It looks something like this:

func giveMeAThing() -> Thing! {
    if !possibleErrorCase() {
        var thing = createAThingForSure()
        return thing
    }
    else {
        crashAndBurn()
        return nil
    }
}

Writing this, it feels a bit hacky, so my question is whether or not this is a good use of implicitly unwrapped Optionals in Swift. Is there a better way to structure my code that will avoid this?

matt

It may be that the piece of the puzzle you are missing is @noreturn. If you declare a function as @noreturn, the compiler abandons any complaints if you don't fulfill the surrounding contract. Thus you can call such a method and not return any value, and the compiler will not complain because it knows we are going to die anyway. For example, fatalError and abort are declared this way.

Thus, the following is legal:

func f() -> String {
    fatalError("oooooops")
}

That compiles even though we fail to fulfill the contract of returning a value, because @noreturn (in the declaration of fatalError) tears up the contract and flushes the pieces down the toilet.

Another possible way of approaching the same issue is to use assert. This crashes and burns for you if a condition is not met. So, in your example:

func giveMeAThing() -> Thing {
    assert(!possibleErrorCase(), "gaaaaah!") // crash and burn if error case
    return createAThingForSure()
}

The downside is that by default all asserts succeed in a shipping app. But it's great during development, and after all your shipping app should not be crashing and burning anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Returning an implicitly unwrapped optional

From Dev

Incrementing an implicitly unwrapped optional

From Dev

Optional Binding on Implicitly Unwrapped Optional

From Dev

Implicitly unwrapped optional made immutable

From Dev

Implicitly unwrapped optional in Apple methods

From Dev

Accessing valueless implicitly unwrapped optional?

From Dev

Implicitly unwrapped optional in Apple methods

From Dev

Generics not working with implicitly unwrapped optional

From Dev

Implicitly unwrapped optional Closure in a method argument

From Dev

Implicitly Unwrapped Optional Types - Possible Typo?

From Dev

What is the point of using implicitly unwrapped optional in this example?

From Dev

Swift unary operator with implicitly unwrapped optional

From Dev

Why is a Swift implicitly unwrapped optional `nil`?

From Dev

Swift unary operator with implicitly unwrapped optional

From Dev

Implicitly unwrapped optional Closure in a method argument

From Dev

Implicitly unwrapped optional from init!() in Swift 3.1

From Dev

Swift's use of Implicitly Unwrapped Optional before availability of nullability

From Dev

Returning Tuple From Delegate (Value of optional type not unwrapped)

From Dev

Returning Tuple From Delegate (Value of optional type not unwrapped)

From Dev

How should I decide if my func should return optional or implicitly unwrapped optional?

From Dev

Implicitly unwrapped multiplication and division

From Dev

Implicitly Unwrapped Optionals and println

From Dev

Implicitly Unwrapped Optionals and println

From Dev

Is there a technical downside to using implicitly-unwrapped optionals but testing for nil vs optional binding?

From Dev

In Apple Swift, in what case(s) would I not want an implicitly unwrapped optional?

From Dev

Swift: Comparing Implicitly Unwrapped Optionals results in "unexpectedly found nil while unwrapping an Optional values"

From Dev

Subscript of a struct doesn't set values when created as an implicitly unwrapped optional

From Dev

Implicitly Unwrapped Optionals in Initialization - Swift

From Dev

How to use Implicitly Unwrapped Optionals?

Related Related

  1. 1

    Returning an implicitly unwrapped optional

  2. 2

    Incrementing an implicitly unwrapped optional

  3. 3

    Optional Binding on Implicitly Unwrapped Optional

  4. 4

    Implicitly unwrapped optional made immutable

  5. 5

    Implicitly unwrapped optional in Apple methods

  6. 6

    Accessing valueless implicitly unwrapped optional?

  7. 7

    Implicitly unwrapped optional in Apple methods

  8. 8

    Generics not working with implicitly unwrapped optional

  9. 9

    Implicitly unwrapped optional Closure in a method argument

  10. 10

    Implicitly Unwrapped Optional Types - Possible Typo?

  11. 11

    What is the point of using implicitly unwrapped optional in this example?

  12. 12

    Swift unary operator with implicitly unwrapped optional

  13. 13

    Why is a Swift implicitly unwrapped optional `nil`?

  14. 14

    Swift unary operator with implicitly unwrapped optional

  15. 15

    Implicitly unwrapped optional Closure in a method argument

  16. 16

    Implicitly unwrapped optional from init!() in Swift 3.1

  17. 17

    Swift's use of Implicitly Unwrapped Optional before availability of nullability

  18. 18

    Returning Tuple From Delegate (Value of optional type not unwrapped)

  19. 19

    Returning Tuple From Delegate (Value of optional type not unwrapped)

  20. 20

    How should I decide if my func should return optional or implicitly unwrapped optional?

  21. 21

    Implicitly unwrapped multiplication and division

  22. 22

    Implicitly Unwrapped Optionals and println

  23. 23

    Implicitly Unwrapped Optionals and println

  24. 24

    Is there a technical downside to using implicitly-unwrapped optionals but testing for nil vs optional binding?

  25. 25

    In Apple Swift, in what case(s) would I not want an implicitly unwrapped optional?

  26. 26

    Swift: Comparing Implicitly Unwrapped Optionals results in "unexpectedly found nil while unwrapping an Optional values"

  27. 27

    Subscript of a struct doesn't set values when created as an implicitly unwrapped optional

  28. 28

    Implicitly Unwrapped Optionals in Initialization - Swift

  29. 29

    How to use Implicitly Unwrapped Optionals?

HotTag

Archive