-
-
Save ympbyc/4996968 to your computer and use it in GitHub Desktop.
#|||||||||||||||||||||||||||||||||||||||| | |
#||||| example of multiple dispatch ||||| | |
#|||||||||||||||||||||||||||||||||||||||| | |
floor = define_class [] | |
carpet = define_class [] | |
ball = define_class [] | |
glass = define_class [] | |
bump = define_generic() | |
define_method bump, [ball, floor], -> 'bounce' | |
define_method bump, [glass, floor], -> 'crash' | |
define_method bump, [undefined, carpet], -> 'silence' | |
bump ball, floor #bounce | |
bump (make ball), (make floor) #bounce | |
bump glass, floor #crach | |
bump ball, carpet #silence | |
bump glass, carpet #silence |
#||||||||||||||||||||||||||||||||||| | |
#||||| example of multimethods ||||| | |
#||||||||||||||||||||||||||||||||||| | |
#fact | |
fact = define_generic() | |
define_method fact, [0], -> 1 | |
define_method fact, ["number"], (n) -> | |
n * fact (n - 1) | |
fact 5 | |
#=> 120 |
# import | |
c = require 'clos' | |
this[name] = fn for name, fn of c |
#||||||||||||||||||||||||||||||||||||||||||| | |
#||||| example of multiple inheritance ||||| | |
#||||||||||||||||||||||||||||||||||||||||||| | |
# domain | |
animal = define_class [], (x) -> | |
slot_exists x, 'voice', 'string' | |
cat = define_class [animal] | |
osx = define_class [], (x) -> | |
slot_exists x, 'hostname', 'string' | |
lion = define_class [cat, osx] | |
# definitions | |
say = define_generic() | |
define_method say, [animal], (a) -> a.voice | |
define_method say, [cat], (c) -> c.voice + ' meow' | |
hostname = define_generic() | |
define_method hostname, [osx], (o) -> o.hostname | |
# main | |
alice = make lion, | |
voice: 'roar' | |
hostname: 'example.com' | |
say alice | |
#=> 'roar meow' | |
hostname alice | |
#=> 'example.com' |
Sorry for a late reply. I haven't been active on github lately.
It seems to be a bug indeed. Somewhere around these lines: https://github.com/ympbyc/js-clos/blob/master/clos.js#L117
I'll look into it when I have time. Thanks
Hello, and sorry too for abandoning js-clos. Lately I came with a minimalistic lisp-on-JS implementation which though already has macros and dynamic scope. Just wanted to share the link now. https://github.com/neverblued/aynil
Thank you @neverblued That's nice.
Glad to hear from the original author!
JSCL seems exciting too.
Dicided this isn't a bug and expected behaviour. Fixing it will involve introducing something like CL's eql-specializer, which isn't a bad idea to pursue but because it's a sketch, if it works for 99% I say it's a pass.
Is this a bug?