Settings

Theme

Show HN: ES7 Decorator library adds types checking, memoization, and more to JS

github.com

66 points by mako-taco 11 years ago · 42 comments

Reader

jeswin 11 years ago

Correct me if I'm wrong, but the necessity for decorators seem to have risen from the ES6 class syntax.

If we were doing classes/functions the old imperative style, we could have simply wrapped the RHS in a standard function that does what the decorator does.

  Animal.prototype.speedup = typeCheckDecorator("number", "number", function(x) {
    this.speed += x;
    return this.speed;
  });
I get that the class syntax makes things easier for people coming from other languages. And maybe makes static analysis easier. But still not sure if it was really needed.
  • tlrobinson 11 years ago

    IMHO the biggest advantage of having class syntax is there's now one "blessed" way to do classical inheritance in JavaScript, whereas until now every JavaScript library implement their own slightly different (potentially incompatible) system.

  • mako-tacoOP 11 years ago

    Static analysis is one of the end goals of this project; the other is generating documentation.

    Decorators are definitely NOT needed for achieving the current functionality.

    • braythwayt 11 years ago

      A beautiful and elegant thing about JavaScript is that decorators are not needed for the current functionality, as you say.

      That being said, some people feel very strongly that languages are better tools when they are less elegant, when they are collections of specialized tools that communicate very specific intentions to readers.

  • the8472 11 years ago

    > And maybe makes static analysis easier.

    > But still not sure if it was really needed.

    That depends on your definition of "needed" of course. Do we need any abstractions? Why not just write assembly, nay, machine code?

    Static analysis enables a lot of tooling (accurate auto-completion, compile-time type checking, null-analysis, ) and can also make life easier for the JIT compilers[1].

    On the one end of the spectrum it's not needed because as long as a language is turing-complete it can do anything any other turing-complete language can do. On the other end it is needed to make your life easier and be more productive by offloading work from your brain to the CPU.

    [1] https://developers.google.com/v8/experiments

    • scotth 11 years ago

      I'd argue this abstraction isn't any higher-level than the function equivalent above. More readable, maybe.

      • the8472 11 years ago

        explicit annotations following a specific syntax can be analyzed by a checker.

        you can't really do that (easily) with ad-hoc typecheck wrappers.

  • braythwayt 11 years ago

    This kind of thing is particularly elegant in CoffeeScript:

        Object.assign Connection.prototype,
          hostname: memoize () -> @host.getRemoteName()
  • loganfsmyth 11 years ago

    The main other benefit of decorators is that they work on property descriptors, and are automatically passed property names and the target object, so you have a bit more info.

    Having the descriptor means for instance that you can switch a value to use a getter function, and that enables a bunch of interesting functionality that you couldn't otherwise do.

    One example that I like is automatically binding methods to the current instance on first property access.

  • TazeTSchnitzel 11 years ago

    They aren't necessary in Python, either.

    But having syntactical support improves readability.

  • z3t4 11 years ago

    One disadvantage of adding syntactic sugars for things you can already do, is that the language gets more difficult to understand. This in turn introduce more bugs and hurts the productivity.

krat0sprakhar 11 years ago

Can someone recommend a simple and easy-to-approach tutorial into ES7 decorators? I have used decorators successfully in my previous Python projects, and I'm eager to start using them in JS. Unfortunately, I haven't yet come across a good guide till now.

untog 11 years ago

Interesting - I wonder whether TypeScript could compile down to this when ES7 is accepted and live in browsers. As it is, I do prefer the syntax of TypeScript.

z3t4 11 years ago

I used to argue for type decorations, like in most C like languages, but then changed my mind.

When programming in a high level language like JavaScript, you should not think about types. Thinking about types will only limit your creativity and problem solving. Instead, you should focus on the logic abstraction.

Also you shouldn't lock yourself into classifications. Instead, just check the input and throw an error if it doesn't fit.

  • mschulze 11 years ago

    >Also you shouldn't lock yourself into classifications. Instead, just check the input and throw an error if it doesn't fit.

    I really don't see the gain here - instead of delegating the typechecks to a tool, the compiler you clutter your code with them. You still have to think about the types, functions/methods (public ones) start with at one, two or more ifs and you even have a hit on runtime perfomance.

    • z3t4 11 years ago

      By helping out the compiler you can get better performance. But I like to think that current JavaScript engines or future ones are quite smart and can optimize beyond that.

      Lets say you are writing software for a cargo company ... The code has well defined classes. The company now wants to expend by including buss goods. But in the code, the terminals and containers will only accept trucks ... It would be better if the terminal code checked if the good fitted etc.

      Thinking about Buss vs Truck is much better then thinking about Array vs Object, but you shouldn't let classifications limit the solutions either.

thomasfoster96 11 years ago

Is something similar to this possible with ES6 proxies? It'd be nice to use a lot of these features outside classes, but since proxies are far harder to polyfill (indeed, I don't think anyone's some it yet) I've only seen then done with decorators within classes.

aaron-lebo 11 years ago

ES/JS continues to get more and more pythonic. Unless you really need a certain library (which is a really good reason), it is getting harder to pick Python over Node for new projects.

skrebbel 11 years ago

This is exactly what I was looking for. Thanks! I can now retire most of my ugly home cooked precondition checking.

salimmadjd 11 years ago

Is there a typo on the Currying example?

let addToFiveAndThree = addFive(3);

Should be:

let addToFiveAndThree = addToFive(3);

I belive.

wslh 11 years ago

It seems like dynamic programming languages end up implementing decorators. To specify types it would be better to specify them in the parameters instead of adding a few lines.

  • reipahb 11 years ago

    I don't think programming languages implement decorators in order to facilitate type checking. They implement decorators because they are a nice generic way to reuse code in many functions / classes.

    Now, once you have support for decorators, they can obviously be reused for type checking, but the linked repository also contains a few more decorators such as a memoization decorator.

    I think those that add type checking decorators do so because it is a convenient way to add a some common code to multiple functions. The alternative for those that add decorators is to manually add a set of type checks in the beginning of each function.

    Of course, if the language supported type checking they would simply use that, but if not, this is a simple way to save some lines of code.

    • braythwayt 11 years ago

        > I don't think programming languages implement decorators in order to facilitate
        > type checking. They implement decorators because they are a nice generic way to
        > reuse code in many functions / classes.
      
      Well, in JavaScript, it’s particularly easy to write decorators as plain old JavaScript functions. Lots of libraries provide implementations that provide memoization or parameter checking on both functions and methods.

      So in the short term, decorators serve only to work around the fact that ES-6 classes actually make this more difficult than ES-5 idioms.

      But in the long term, a decorator can be extended to deal with static analysis, while functions provid eno such capability, so we’d be back to having “magic comments,” or inventing a new kind of pragma.

      So, I’d say that the motivation is to lay the groundwork for things involving static analysis or manipulation of programs. Like type checking, or even manipulation of the AST.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection