Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ObjS: now with self, _cmd arguments
import Cocoa
typealias Selector = String
enum IMP {
case accessor((Selector->IMP, Selector)->((Selector)->IMP)?)
case asInteger((Selector->IMP, Selector)->Int?)
case methodMissing((Selector->IMP, Selector)->((Selector)->IMP)?)
case mutator((Selector->IMP, Selector, Selector->IMP)->Void)
case description((Selector->IMP, Selector)->String?)
}
typealias Object = Selector -> IMP
func DoesNothing()->Object {
var _self : Object! = nil
func myself (selector: Selector)->IMP {
return IMP.methodMissing({(this, _cmd) in assertionFailure("method missing: \(_cmd)"); return nil;})
}
_self = myself
return _self
}
let o : Object = DoesNothing()
infix operator .. {}
func .. (receiver: Object?, _cmd:Selector) -> IMP? {
if let this = receiver {
let method = this(_cmd)
switch(method) {
case .methodMissing(let f):
return f(this, _cmd).._cmd
default:
return method
}
}
else {
return nil
}
}
nil.."asInteger"
infix operator {}
func (receiver: Object?, _cmd:Selector) -> Object? {
if let imp = receiver.._cmd {
switch (imp) {
case .accessor(let f):
return f(receiver!, _cmd)
default:
return nil
}
} else {
return nil
}
}
func ℹ︎(receiver:Object?)->Int? {
if let imp = receiver.."asInteger" {
switch(imp) {
case .asInteger(let f):
return f(receiver!, "asInteger")
default:
return nil
}
} else {
return nil
}
}
infix operator {}
func (tuple:(receiver: Object?, command: Selector), value:Object) -> Void {
if let imp = tuple.receiver..tuple.command {
switch(imp) {
case .mutator(let f):
f(tuple.receiver!, tuple.command, value)
default:
return
}
}
}
func 📓(receiver:Object?) -> String? {
if let imp = receiver.."description" {
switch(imp) {
case .description(let f):
return f(receiver!, "description")
default:
return nil
}
} else {
return nil
}
}
func Integer(x: Int, proto: Object) -> Object {
var _self : Object! = nil
let _x = x
func myself(selector:Selector) -> IMP {
switch(selector) {
case "asInteger":
return IMP.asInteger({ _ in return _x })
case "description":
return IMP.description({ _ in return "\(_x)" })
default:
return proto(selector)
}
}
_self = myself
return _self
}
let theMeaning = Integer(42, o)
ℹ︎(theMeaning)!
func Proxy(target:Object)->((_cmd:Selector)->IMP) {
var _self : Object! = nil
var _target = target
func myself(selector:Selector) -> IMP {
return IMP.methodMissing({ _ in return _target })
}
_self = myself
return _self
}
let proxyMeaning = Proxy(theMeaning)
📓(proxyMeaning)
func Point(x: Int, y: Int, proto: Object)->((_cmd:Selector)->IMP) {
var _self : Object! = nil
var _x = Integer(x,o), _y = Integer(y,o)
func myself (selector:Selector) -> IMP {
switch (selector) {
case "x":
return IMP.accessor({ _ in
return _x
})
case "y":
return IMP.accessor({ _ in
return _y
})
case "setX:":
return IMP.mutator({ (this, aSelector, newX) in
_x = newX
})
case "setY:":
return IMP.mutator({ (this, aSelector, newY) in
_y = newY
})
case "description":
return IMP.description({ (this, aSelector) in
let xii = ℹ︎(this"x")!
let yii = ℹ︎(this"y")!
return "(\(xii),\(yii))"
})
default:
return proto(selector)
}
}
_self = myself
return _self
}
let p = Point(3, 4, o)
📓(p)
ℹ︎(p"x")
(p,"setX:")(Integer(1,o))
ℹ︎(p"x")
📓(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment