Last active
August 29, 2015 14:18
-
-
Save jampekka/64977103d193f6252f2c to your computer and use it in GitHub Desktop.
Classhack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Motivation for this madness: I often find that simply a function is | |
# a nice interface objects. However, often later on a need arises that | |
# we need also something else in a return value / argument, which currently | |
# calls for refactoring of every call site. In Python this can be hacked | |
# around using __call__. This would be a way to hack around it in LiveScript. | |
# | |
# And anyway I think having separate "classes" and "objects" is a bit stupid | |
# historical artefact. KISS! | |
fobj = (f) -> (...args) -> | |
me = {} | |
callable = f.apply(me, args) ? me | |
# Would maybe be nicer using proto | |
for key, value of me | |
callable[key] = value | |
return callable | |
AFunctionObject = fobj (@member_arg, private_arg, @other_arg='default') -> | |
@someOtherStuff = 'whatever' | |
@printArg = ~> | |
console.log @member_arg | |
# The "default function" | |
-> | |
console.log "I'm just a function!" | |
func = AFunctionObject 'foo', 'bar | |
func! | |
func.printArg! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment