[Swift] Another way to use Result?

First conclusion

How about using Result in a computed property? (* Suggestion, not recommendation)

I want to throw even in Computed property!

You can't write code like this in Swift (as of Swift 5):

enum E: Error {
  case someError
}

struct S {
  var someProperty: throws Int { //⛔️ Can't compile
    if Bool.random() {
      return 0
    } else {
      throw E.someError
    }
  }
}

That's because you can't use the keyword throws for properties in the first place. throws can only be used with func (or closures), so if you want to do the same, you usually do this:

enum E: Error {
  case someError
}

struct S {
  func someProperty() throws -> Int {
    if Bool.random() {
      return 0
    } else {
      throw E.someError
    }
  }
}

… By the way, isn't there a way to use Result? ??

enum E: Error {
  case someError
}

struct S {
  var someProperty: Result<Int, Error> {
    return .init(catching: {
      if Bool.random() {
        return 0
      } else {
        throw E.someError
      }
    })
  }
}

Benefits of using Result?

There is no particular merit, but ... If you dare to mention:

-* Realize the feeling that "I want the property to be a property. I don't want to use func. "*. --Evaluation can be delayed until you call .get ().

in conclusion

It's just an idea, but I personally think that it may be convenient (sometimes) to use it with a property * that is not * public (∴ completed within that module).

Recommended Posts

[Swift] Another way to use Result?
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
[Swift] How to use Tab Bar Controller
[Swift] How to use one option alert
Note how to use Swift super basic TableView
Super easy way to use enum with JSP
How to use Map
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
PATH to use docker-slim
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
[Swift] Use UserDefaults to save data in the app
[Swift] Let's use extension
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use a foreign key with FactoryBot ~ Another solution
Use WHERE to retrieve the result calculated using the Mysql syntax
[Swift] How to install Firebase ~ How to use Realtime Database & Cloud Firestore
[Swift] Use UIProgressView to display progress + UIAlertController to notify processing completion
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use JUnit (beginner)
How to use Ruby return
[Swift] Let's use Segmented Control
[Rails] How to use enum
Use Swift Package on Playground
How to use @Builder (Lombok)
How to use java class
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
Use swift Filter and Map
Let's use Swift Firebase Firebase Auth
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
[java] Reasons to use static
Rbenv command to use Ruby