importFoundation// We implemented a module named Wonderful.// Extension name will be named as wf// Step 0.// Encapsulate a type which is unknown until now.structExtensible<EncapsulatedType> {
let type: EncapsulatedType
init(_ type: EncapsulatedType) {
self.type = type
}
}
// Step 1.// Extension a type to add two properties. a static var and a varprotocolWonderfulExtension {
associatedtype ExtendedType
// static entry pointstaticvar wf: Extensible<ExtendedType>.Type { getset }
// instance entry pointvar wf: Extensible<ExtendedType> { getset }
}
// Step 2.extensionWonderfulExtension {
staticvar wf: Extensible<Self>.Type {
set { }
get { return Extensible<Self>.self }
}
var wf: Extensible<Self> {
set { }
get { return Extensible(self) }
}
}
// Step3.// Extend the type we want to, which the type will have wf var.structOrdinary {}
extensionOrdinary: WonderfulExtension {}
// Step4.// Restrict methods in the scope of type we want to extendextensionExtensiblewhere EncapsulatedType == Ordinary {
funcwow() {
print("Wow, Wonderful extension.")
}
}
// Use methods of wf extensionlet obj = Ordinary()
obj.wf.wow()