Skip to content

Instantly share code, notes, and snippets.

@jampekka
Last active Aug 29, 2015
Embed
What would you like to do?
Classhack
# 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