News for hackers. Stuff that matters.
Re:TC39, ECMAScript, and the Future of JavaScript by JoshGlazebrook on Monday June 26, 2017 @07:07PM (#14641181)
Really looking forward to class decorators.
Re:TC39, ECMAScript, and the Future of JavaScript by abritinthebay on Monday June 26, 2017 @07:59PM (#14641458)
Decorators are one of those things that I've not seen a use case that sells them to me.

Like... I'm sure one day I'll see something and it'll just click, but not there yet.

Re:TC39, ECMAScript, and the Future of JavaScript by mmargerum on Tuesday June 27, 2017 @07:25AM (#14644319)
When marshaling classes in and out of xml/json decarators can be added to properties to change the name of the equivalent Json field value as well as indicating if it's optional or not. Orms use decators to map object to relational tables the may have different field names, expressing key values, etc
Re:TC39, ECMAScript, and the Future of JavaScript by abritinthebay on Tuesday June 27, 2017 @11:37AM (#14646385)
None of that sounds particularly hard, complex, or tricky without decorators though.

Like, I get they can be used for stuff but why are they a better option?

So far - based on the responses here - it appears to be syntax for syntax sake and no practical benefit.

Re:TC39, ECMAScript, and the Future of JavaScript by mmargerum on Friday June 30, 2017 @10:38AM (#14671009)
It's just sugar but it's less code and I find it easy to read at least in go.
Re:TC39, ECMAScript, and the Future of JavaScript by abritinthebay on Thursday July 06, 2017 @03:51PM (#14713334)
It’s not really less code though. It’s about the same but with an syntax that obfuscates where the logic happens.

Terse at the usage point yes, but it swaps clarity for brevity. A sometimes useful trade off to be sure but... not sure I’ve seen any demonstration of benefits here.

Re:TC39, ECMAScript, and the Future of JavaScript by danielbarla on Tuesday June 27, 2017 @03:39AM (#14643230)
Well, in C#-land, we often use / abuse attributes to get information about a class or property at runtime. This way, framework or library code can inspect entities that you've authored, and dynamically deal with them. Perhaps a decent example is validation, where a declarative style has gained a lot of popularity. A JS example might be:

  @required firstName;
This simply declares the intent / rule, but does not provide the implementation.

We can argue terseness vs verbosity, implicitness vs explicitness all day - I'd argue that for certain basic things that you do highly frequently, a terse and implicit style is not a bad thing. It tends to increase the signal-to-noise ratio of your code, allowing the developer to focus on things that really are interesting, and not have to read through a lot of boilerplate.

Re:TC39, ECMAScript, and the Future of JavaScript by abritinthebay on Tuesday June 27, 2017 @03:45AM (#14643246)
Hmmm a required flag is a nice idea (but better exposed using interfaces as an idiom I’d argue) but it’s not selling me on the wider use case.

If we were talking about very basic things then yes, sure, but I’d also argue that if they are truly basic then we could restrict decorators to built-in ones only (as those are the basics).

Much like there are “magic” Symbols like Symbol.iterator.

Re:TC39, ECMAScript, and the Future of JavaScript by danielbarla on Tuesday June 27, 2017 @03:58AM (#14643287)
That's interesting. My knowledge of JS is a bit lacking, so I'm struggling to see the equivalence of the interfaces approach. E.g., is it possible to parametrize interfaces with actual values, such as @maxLength(20)? Also, how do you apply interfaces down to the field-level, where they are of primitive types, such as number or string?
Re:TC39, ECMAScript, and the Future of JavaScript by abritinthebay on Tuesday June 27, 2017 @11:35AM (#14646345)
That feels like a job for getters/setters, no?
Re:TC39, ECMAScript, and the Future of JavaScript by danielbarla on Wednesday June 28, 2017 @01:33AM (#14651915)
I guess it depends what style you favour. I can't imagine using getters/setters like that in JS today, and not have major pains from it (things like not being able to use data binding frameworks easily, etc).
Re:TC39, ECMAScript, and the Future of JavaScript by saghm on Monday June 26, 2017 @11:46PM (#14642421)
I'm the same way, except less sure that I'll find something that will change my mind.
Re:TC39, ECMAScript, and the Future of JavaScript by abritinthebay on Tuesday June 27, 2017 @03:47AM (#14643255)
Maybe “sure” was overly optimistic. “Hopeful” might be better put.

I like the idea but I’ve yet to see a case that leads to better Code than other implementations.

Re:TC39, ECMAScript, and the Future of JavaScript by Matthias247 on Tuesday June 27, 2017 @05:22AM (#14643595)
In angular2 they are for example used for dependency injection, which works really well.

Other use cases that I see are for example automatically generating [de]serialization code for members based on decorators and their values (like how Annotations are used in C# and Java serialization frameworks or tags in Go).

There are also web frameworks popping up which use decorators for routing and parameter binding in a similar fashion how some Java frameworks work.

Re:TC39, ECMAScript, and the Future of JavaScript by abritinthebay on Tuesday June 27, 2017 @11:41AM (#14646419)
So syntax to his bad application design & code patterns behind? That’s an argument against them surely?
Re:TC39, ECMAScript, and the Future of JavaScript by solidr53 on Tuesday June 27, 2017 @12:17AM (#14642533)
Re:TC39, ECMAScript, and the Future of JavaScript by abritinthebay on Tuesday June 27, 2017 @01:28AM (#14642787)
That... doesn't help. From what I see there I get harder to follow, non-idiomatic to anything in JS, code but the bonus is a terser syntax?

Great...?

I really hope there's a better use case for them because this is not selling me. Literally none of that post is a compelling case.

I get the cleaner syntax, that's nice, but it also means now I have to look up what that does. And that decorated properties example? Yikes that's awful - it looks like a code-smell frankly.

But all that initial urrrgh factor could be removed by showing me a use case that makes the code significantly more elegant to do `@foo bar` rather than `foo(bar)`.

Re:TC39, ECMAScript, and the Future of JavaScript by spleeder on Monday June 26, 2017 @07:47PM (#14641393)
I for one am not. What is wrong with calling a plain function?
Re:TC39, ECMAScript, and the Future of JavaScript by SimeVidas on Monday June 26, 2017 @08:20PM (#14641570)
You need to use `.bind()` to set the context, or switch to using arrow functions. Both methods are sub-optimal. `@autobind` solves that.
Re:TC39, ECMAScript, and the Future of JavaScript by tlrobinson on Monday June 26, 2017 @09:12PM (#14641814)
Why is

   @autobind
   foo() {
     // ...
   }
more optimal than

   foo = () => {
     // ...
   }

?
Re:TC39, ECMAScript, and the Future of JavaScript by jopsen on Tuesday June 27, 2017 @03:40AM (#14643232)
Please, Nothing is ever _more optimal_.
Re:TC39, ECMAScript, and the Future of JavaScript by k__ on Monday June 26, 2017 @09:08PM (#14641794)
Class properties solve that too, with less magic IMO.
Re:TC39, ECMAScript, and the Future of JavaScript by dtzur on Tuesday June 27, 2017 @02:19AM (#14642916)
Decorators are awesome! Stop this blasphemy.
Re:TC39, ECMAScript, and the Future of JavaScript by solidr53 on Tuesday June 27, 2017 @12:15AM (#14642527)
Bad example, checkout mobx or core-decorators.

@computed get value() { return this.a + 1; } @readonly bar = 5;

Re:TC39, ECMAScript, and the Future of JavaScript by RonanTheGrey on Tuesday June 27, 2017 @02:12PM (#14647765)
The use of decorators in Anguar 2/4 is also very well done, IMHO. They allow your class to focus on its business logic, rather than the minute details of how Angular expects it to interact with the rest of Angular.

There's a reason all the most common languages have them, concealing complexity is a way to reduce cognitive overload and therefore, bugs.

Re:TC39, ECMAScript, and the Future of JavaScript by andreineculau on Monday June 26, 2017 @08:02PM (#14641479)
Oh please. Decorators will never beat plain old functions when it comes to flexibility.
Re:TC39, ECMAScript, and the Future of JavaScript by rhencke on Monday June 26, 2017 @08:30PM (#14641607)
C will never beat plain old assembly when it comes to flexibility.
Re:TC39, ECMAScript, and the Future of JavaScript by hajile on Monday June 26, 2017 @08:51PM (#14641712)
Hardly a fair comparison. Decorators exist in Python and other languages primarily because they either don't have lambdas or have bad ones. JS doesn't suffer from those problems, so the mental overhead of the additional syntax has a much lower payoff (and one that I believe isn't worth the trouble).
Re:TC39, ECMAScript, and the Future of JavaScript by tlrobinson on Monday June 26, 2017 @09:10PM (#14641804)
Really wish they had included function decorators too.

https://github.com/wycats/javascript-decorators/issues/4

Re:TC39, ECMAScript, and the Future of JavaScript by netcraft on Monday June 26, 2017 @11:15PM (#14642324)
I wish we could get macros then we could make our own decorators... (macro support in typescript would be awesome)