News for hackers. Stuff that matters.
Re:Show HN: ES7 Decorator library adds types checking, memoi... by jeswin on Sunday May 31, 2015 @09:35AM (#9634111)
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.
Re:Show HN: ES7 Decorator library adds types checking, memoi... by tlrobinson on Sunday May 31, 2015 @02:08PM (#9634951)
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.
Re:Show HN: ES7 Decorator library adds types checking, memoi... by mako-taco on Sunday May 31, 2015 @09:40AM (#9634131)
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.

Re:Show HN: ES7 Decorator library adds types checking, memoi... by braythwayt on Sunday May 31, 2015 @10:37AM (#9634277)
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.

Re:Show HN: ES7 Decorator library adds types checking, memoi... by the8472 on Sunday May 31, 2015 @03:38PM (#9635265)
> 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

Re:Show HN: ES7 Decorator library adds types checking, memoi... by scotth on Sunday May 31, 2015 @04:02PM (#9635338)
I'd argue this abstraction isn't any higher-level than the function equivalent above. More readable, maybe.
Re:Show HN: ES7 Decorator library adds types checking, memoi... by the8472 on Sunday May 31, 2015 @04:06PM (#9635353)
explicit annotations following a specific syntax can be analyzed by a checker.

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

Re:Show HN: ES7 Decorator library adds types checking, memoi... by braythwayt on Sunday May 31, 2015 @10:41AM (#9634290)
This kind of thing is particularly elegant in CoffeeScript:

    Object.assign Connection.prototype,
      hostname: memoize () -> @host.getRemoteName()
Re:Show HN: ES7 Decorator library adds types checking, memoi... by loganfsmyth on Sunday May 31, 2015 @12:36PM (#9634636)
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.

Re:Show HN: ES7 Decorator library adds types checking, memoi... by TazeTSchnitzel on Sunday May 31, 2015 @11:27AM (#9634434)
They aren't necessary in Python, either.

But having syntactical support improves readability.

Re:Show HN: ES7 Decorator library adds types checking, memoi... by z3t4 on Sunday May 31, 2015 @02:07PM (#9634944)
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.