Result Type की जानकारी
Result एक special box है जो हमेशा दो चीज़ों में से एक रखता है: या तो अंदर एक value वाली एक success, या अंदर एक error वाली एक failure।
Syntax
func functionName() -> Result<SuccessType, ErrorType> {
return .success(value) // or .failure(error)
}
switch functionName() {
case .success(let value): /* use value */
case .failure(let error): /* handle error */
}
try functionName().get()
Creating and Switching on a Result
एक Result<Success, Failure> value या तो .success या .failure के रूप में बनाई जाती है, और एक switch से handle की जाती है जो हर case की associated value bind करता है।
उदाहरण: Creating and Switching on a Result
// Define an enum `MathError` listing its possible cases
enum MathError: Error {
// Case `divideByZero`
case divideByZero
}
// Define the function `divide` taking `a`, `b` and returning a `Result<Int, MathError>`
func divide(_ a: Int, by b: Int) -> Result<Int, MathError> {
// Check whether `b == 0`
if b == 0 {
// Return `.failure(.divideByZero)` from this function
return .failure(.divideByZero)
}
// Return `.success(a / b)` from this function
return .success(a / b)
}
// Declare a constant `result`, set to `divide(10, by: 2)`
let result = divide(10, by: 2)
// Switch on `result` and branch by case
switch result {
// Case: `.success(let value)`
case .success(let value):
// Print "Result: \(value)" to the console
print("Result: \(value)")
case .failure(let error):
print("Error: \(error)")
}
Login to try C/C++/Java/PHP code in the editor
Converting a Result to a Throwing Call
Result एक .get() method प्रदान करता है जो इसे वापस एक throwing expression में convert करता है, दोनों error-handling styles को bridge करते हुए।
उदाहरण: Converting a Result to a Throwing Call
// Define an enum `MathError` listing its possible cases
enum MathError: Error {
// Case `divideByZero`
case divideByZero
}
// Define the function `divide` taking `a`, `b` and returning a `Result<Int, MathError>`
func divide(_ a: Int, by b: Int) -> Result<Int, MathError> {
b == 0 ? .failure(.divideByZero) : .success(a / b)
}
do {
// Declare a constant `value`, set to `try divide(9, by: 0).get()`
let value = try divide(9, by: 0).get()
// Print `value` to the console
print(value)
} catch {
// Print "Caught via get(): \(error)" to the console
print("Caught via get(): \(error)")
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- यह भूल जाना कि
Resultको दो generic types चाहिए -- success type और failure (error) type -- दोनों इसकी declaration में specified। - किसी
Resultपरswitchउपयोग करना लेकिन यह भूल जाना कि हर case अपनी खुद की associated value bind करता है (.success(let value)/.failure(let error))। - किसी function के return type के लिए
Resultचुनना जब synchronous code के लिए एक simplethrowsfunction simpler होता --Resultoutcomes को store या pass around करने के लिए ज़्यादा चमकता है, खासकर async callbacks में।
चैप्टर सारांश
Result<Success, Failure>या तो.success(value)या.failure(error)represent करता है।- यह उन outcomes represent करने के लिए विशेष रूप से उपयोगी है जिन्हें store या pass around करना है, सिर्फ तुरंत handle नहीं।
switchकिसीResultके दोनों cases handle करने का natural तरीका है।Resultको इसकेFailuretype कोErrorconform करने की ज़रूरत है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: