Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension Person {
- public fun waldo() -> Waldo { return _Waldo(this: self) }
- class _Waldo: Waldo {
- var this:Person
- private static var type_map: [String:Any.Type] = [
- "name": String.self,
- "birthday": Date.self,
- "age": Int.self,
- ]
- init (this: Person) {
- self.this = this
- }
- public func type (for key: String) -> Any.Type? {
- return _Waldo.type_map[key]
- }
- public func get<T> (for key: String, default value: T? = nil) -> T? {
- switch key {
- case "name": return this.name as? T
- case "birthday": return this.birthday as? T
- case "age": return this.age as? T
- default:
- return value
- }
- }
- public func set<T> (_ key: String, to value: T? = nil) {
- switch key {
- case "name":
- if this.name is T,
- let tv = value as? String {
- this.name = tv
- }
- case "birthday":
- if this.birthday is T,
- let tv = value as? Date {
- this.birthday = tv
- }
- default:
- return
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment