Generics में where Clauses
Generics में एक where clause पहले से दिए basic constraints से आगे उपयोग हो रहे types के बारे में extra fine-print rules जोड़ता है।
Syntax
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
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])
Login to try C/C++/Java/PHP code in the editor
A where Clause on an Extension
एक extension को where से constrained किया जा सकता है ताकि यह सिर्फ तब apply हो जब generic type एक specific condition पूरी करे, जैसे इसके elements comparable हों।
उदाहरण: A where Clause on an Extension
// 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())
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. #}
आम गलतियां
- इस generics
where(generic types पर constraints जोड़ने) को पहले के chapters से loop/switchwhereके साथ confuse करना; दोनों filter करते हैं, लेकिन बहुत अलग contexts में। - दो अलग generic placeholders के बीच एक relationship मांगने की कोशिश करना (जैसे उनके elements match करने चाहिए) बिना इसे express करने के लिए एक
whereclause के। - किसी function signature को constraints से overload करना जब extension methods के लिए एक simpler
where Self.Element == ...clause इसे ज़्यादा clearly express करता।
चैप्टर सारांश
- एक generic parameter list के बाद एक
whereclause अतिरिक्त constraints जोड़ता है। - यह आमतौर पर दो अलग generic types को एक related requirement share करने की मांग के लिए उपयोग होता है।
whereclauses 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: