रिजल्ट टाइप: Difference between revisions

From Vigyanwiki
No edit summary
No edit summary
Line 24: Line 24:
         println!("Great, we could pet the cat!");
         println!("Great, we could pet the cat!");
   } else {
   } else {
println!("Oh no, we couldn't pet the cat!");


fn pet_cat () -> परिणाम <(), स्ट्रिंग> {
    println!("Oh no, we couldn't pet the cat!");
     अगर CAT_FOUND {
      }
        ठीक है(())
  }
    } अन्य {
      fn pet_cat() -> Result<(), String> {
        एरर (स्ट्रिंग :: फ्रॉम (बिल्ली कहीं नहीं मिली))
     if CAT_FOUND {
    }
}
}
</वाक्यविन्यास हाइलाइट>
</वाक्यविन्यास हाइलाइट>

Revision as of 00:32, 26 February 2023

कार्यात्मक प्रोग्रामिंग में, परिणाम एक मोनाडिक प्रकार होता है जिसमें इसका मान त्रुटि कोड होता है। जो असामान्यता हैंडलिंग का सहारा लिए बिना त्रुटियों का विधारण करने की विधि प्रदान करते हैं; जब कोई फ़ंक्शन विफल हो सकता है, परिणाम प्रकार का पुनरावृत्ति करता है, प्रोग्रामर को अपेक्षित परिणाम तक पहुंच प्राप्त करने से पूर्व, सफलता या विफलता मार्ग पर विचार करने के लिए विवश किया जाता है; यह त्रुटिपूर्ण प्रोग्रामर धारणा की संभावना को समाप्त करता है।

उदाहरण


रस्ट

परिणाम वस्तु में विधियाँ हैं।- is_ok() और is_err()

   const CAT_FOUND: bool = true;



   fn main() {
       let result = pet_cat();
  if result.is_ok() {
       println!("Great, we could pet the cat!");
 } else {
   println!("Oh no, we couldn't pet the cat!");
     }
  }
      fn pet_cat() -> Result<(), String> {
   if CAT_FOUND {

} </वाक्यविन्यास हाइलाइट>

यह भी देखें

संदर्भ

  1. "Result · An Introduction to Elm". guide.elm-lang.org.
  2. "Data.Either". hackage.haskell.org.
  3. "Error Handling – OCaml". ocaml.org.
  4. "std::result - Rust". doc.rust-lang.org.
  5. "Scala Standard Library 2.13.3 - scala.util.Either". www.scala-lang.org. Retrieved 9 October 2020.
  6. "Apple Developer Documentation". developer.apple.com.