Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Cool! Sucks that it has to be so stringly typed, but I can't see another alternative.

One concern:

"Each pattern will be tried in order until a match is found."

As far as I know, ECMAScript doesn't specify any enumerations on objects that obey an order... browsers do syntactic order, but it's by no means a guarantee. (e.g. http://stackoverflow.com/a/280861)



You are correct. There is not a guarantee for the future, though I doubt this behavior will change for V8 (this is currently node only). If that scares you, you can use the combinator syntax (check out the readme towards the bottom). The object literal syntax is just sugar for using combinators.

edit: BTW, if you want some precedence in a major library relying on this behavior, look no further than Backbone's router, specifically the `_bindRoutes` method.


Sucks that it has to be so stringly typed, but I can't see another alternative.

Multimethods would be an alternative to this type of ad-hoc polymorphism. I've encoded immutable multimethod environments in my new library, bilby.js:

https://github.com/pufuwozu/bilby.js

Lets you write things like this:

    var env = λ.environment()
        .method('length', λ.isArray, function(a) {
            return a.length;
        })
        .method('length', λ.isString, function(s) {
            return s.length;
        })
        .property('empty', function(o) {
            return !this.length(o);
        });

    env.empty([]) == true;
    env.empty([1, 2, 3]) == false;
Where isArray and isString are any functions that return true/false based on the input arguments. The environment then dispatches whichever method that has a predicate return true first.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: