👔NonNominalTypeWrapper
💾 程式:paiza.io ⬆️ 需要: HasMirrors
// --------------------------------
// ⭐ NonNominalTypeWrapper
// --------------------------------
/// non-nominal type:
/// ⛔ - can't be extended.
/// ⛔ - can't conform to protocols.
/// use this type to wrap non-nominal types, so they can do these things.
public struct NonNominalTypeWrapper<T> {
let subject: T // T is non-nominal (i.e. Any, Tuple)
}
/// convenience init
/// usage: `NonNominalTypeWrapper(instance)`
extension NonNominalTypeWrapper {
// ⭐ 注意:這裡不能寫成 init<T>
// 不然會引進「新的型別參數 `T`」(是的,跟「舊的型別參數 `T`」
// 名字一模一樣),然後產生「令人傻眼的錯誤訊息」:
// 「⛔ cannot assign value of type 'T' to type 'T'」
public init(_ subject: T){
self.subject = subject
}
}
/// ⭐ custom extension (Loggable conformance)
extension NonNominalTypeWrapper: Loggable {
@discardableResult
public func log() -> Self {
print(String(describing: self.subject))
return self
}
@discardableResult
public func reflect() -> Self {
print(String(reflecting: self.subject))
return self
}
// mirror (object tree)
@discardableResult
public func mirror() -> Self {
mirrorChildren(of: self.subject)
}
}
Last updated
Was this helpful?