Hacker News new | ask | show | jobs
by jeswin 4035 days 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.
7 comments

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.
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.

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.

> 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

I'd argue this abstraction isn't any higher-level than the function equivalent above. More readable, maybe.
explicit annotations following a specific syntax can be analyzed by a checker.

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

This kind of thing is particularly elegant in CoffeeScript:

    Object.assign Connection.prototype,
      hostname: memoize () -> @host.getRemoteName()
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.

They aren't necessary in Python, either.

But having syntactical support improves readability.

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.