Protocol Composition क्या है
Protocol composition आपको यह कहने देता है कि एक type को एक साथ एक से ज़्यादा checklist से items check करने चाहिए, इन्हें एक ampersand से combine करते हुए।
Syntax
func functionName(_ param: ProtocolA & ProtocolB) {
// param has members of both protocols
}
func other(_ param: ProtocolA & ProtocolB & ProtocolC) { }
Requiring Multiple Protocols with &
एक function parameter जो ProtocolA & ProtocolB typed है सिर्फ दोनों protocols को एक साथ conform करने वाली values accept करता है।
उदाहरण: Requiring Multiple Protocols with &
// Define a protocol `Named` describing required behavior
protocol Named {
var name: String { get }
}
// Define a protocol `Aged` describing required behavior
protocol Aged {
var age: Int { get }
}
// Define a struct `Person`
struct Person: Named, Aged {
var name: String
var age: Int
}
// Define the function `describe` taking `value` and returning a String
func describe(_ value: Named & Aged) -> String {
// Return `"\(value.name) is \(value.age) years old"` from this function
return "\(value.name) is \(value.age) years old"
}
// Declare a constant `person`, set to `Person(name: "Lee", age: 40)`
let person = Person(name: "Lee", age: 40)
// Print `describe(person)` to the console
print(describe(person))
Login to try C/C++/Java/PHP code in the editor
Composition with Three or More Protocols
Protocol composition दो protocols तक सीमित नहीं है -- अतिरिक्त & operators के साथ कितने भी combine किए जा सकते हैं।
उदाहरण: Composition with Three or More Protocols
// Define a protocol `Named` describing required behavior
protocol Named { var name: String { get } }
// Define a protocol `Aged` describing required behavior
protocol Aged { var age: Int { get } }
// Define a protocol `Employed` describing required behavior
protocol Employed { var job: String { get } }
// Define a struct `Worker`
struct Worker: Named, Aged, Employed {
var name: String
var age: Int
var job: String
}
// Define the function `introduce` taking `value`
func introduce(_ value: Named & Aged & Employed) {
// Print "\(value.name), \(value.age), works as \(value.job)" to the console
print("\(value.name), \(value.age), works as \(value.job)")
}
introduce(Worker(name: "Kai", age: 33, job: "Engineer"))
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. #}
आम गलतियां
- कई protocols मांगने के लिए inheritance-style syntax उपयोग करने की कोशिश करना; composition protocol names के बीच
&उपयोग करता है, एक type annotation में commas नहीं। - यह भूल जाना कि एक composed type (
ProtocolA & ProtocolB) को एक instance से दोनों protocols conform करने की मांग करता है, सिर्फ एक की नहीं। - कई composed protocols के साथ एक function signature को overcomplicate करना जब requirements combine करने वाला एक single custom protocol ज़्यादा clear होता।
चैप्टर सारांश
ProtocolA & ProtocolBएक type describe करता है जो एक साथ दोनों protocols conform करता है।- यह आमतौर पर कई capabilities मांगने के लिए एक parameter type के रूप में उपयोग होता है।
- Composition class inheritance से अलग है, जो सिर्फ एक superclass allow करता है।
- आप
&के साथ कितने भी protocols एक साथ compose कर सकते हैं।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: