← Back to Swift Course | Chapter 12: Generics & Advanced Types | Lesson 5 of 7

Generics में where Clauses

Generics में एक where clause पहले से दिए basic constraints से आगे उपयोग हो रहे types के बारे में extra fine-print rules जोड़ता है।
Syntax
swift
func functionName<A: Sequence, B: Sequence>(_ a: A, _ b: B) where A.Element == B.Element {
    // both hold the same element type
}

extension Type where Element: Comparable {
    func method() { }
}

Using where to Relate Two Generic Types

एक where clause मांग सकता है कि दो अलग generic sequences बिल्कुल एक जैसे type के elements रखें, जो अकेले plain angle-bracket constraints express नहीं कर सकते।

उदाहरण: Using where to Relate Two Generic Types

markup
func printMatchingPairs<A: Sequence, B: Sequence>(_ first: A, _ second: B) where A.Element == B.Element, A.Element: Equatable {
    for (a, b) in zip(first, second) where a == b {
        // Print "Match: \(a)" to the console
        print("Match: \(a)")
    }
}
printMatchingPairs([1, 2, 3], [3, 2, 1])

A where Clause on an Extension

एक extension को where से constrained किया जा सकता है ताकि यह सिर्फ तब apply हो जब generic type एक specific condition पूरी करे, जैसे इसके elements comparable हों।

उदाहरण: A where Clause on an Extension

markup
// Extend `Array` with additional functionality
extension Array where Element: Comparable {
    // Define the function `isSorted` with no parameters and returning a Bool
    func isSorted() -> Bool {
        // Loop over `1..<count where self[i] < self[i - 1]`, binding each item to `i`
        for i in 1..<count where self[i] < self[i - 1] {
            // Return `false` from this function
            return false
        }
        // Return `true` from this function
        return true
    }
}
// Print `[1, 2, 3].isSorted()` to the console
print([1, 2, 3].isSorted())
// Print `[3, 1, 2].isSorted()` to the console
print([3, 1, 2].isSorted())
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. #}
आम गलतियां
  1. इस generics where (generic types पर constraints जोड़ने) को पहले के chapters से loop/switch where के साथ confuse करना; दोनों filter करते हैं, लेकिन बहुत अलग contexts में।
  2. दो अलग generic placeholders के बीच एक relationship मांगने की कोशिश करना (जैसे उनके elements match करने चाहिए) बिना इसे express करने के लिए एक where clause के।
  3. किसी function signature को constraints से overload करना जब extension methods के लिए एक simpler where Self.Element == ... clause इसे ज़्यादा clearly express करता।
चैप्टर सारांश
  • एक generic parameter list के बाद एक where clause अतिरिक्त constraints जोड़ता है।
  • यह आमतौर पर दो अलग generic types को एक related requirement share करने की मांग के लिए उपयोग होता है।
  • where clauses specific generic instantiations तक constrained extensions में विशेष रूप से उपयोगी हैं।
  • वे base generic declaration को simple रखते हुए ज़रूरत पड़ने पर precise requirements layer करते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.