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

But decorators are just syntactic sugar around functions. You can have them now with;

    var deprecated = function(fn) {
       console.warn("Deprecated!");
       fn.apply(this, arguments);
    };
then

    var oldMethod = deprecated(function() {
    });
In ES7 you'll be able to call decorators with the new syntax:

    @deprecated
    function oldMethod {
    }
But nothing stopping you writing modular code now. The complaint about 'I have to write console.warn everywhere' seems to have an obvious answer; 'no you don't'.

As for other issues with ES6+7, the new syntax trades a few characters for a lack of clarity about what is actually happening.



Thanks for writing it out, but you've actually illustrated exactly my complaint. When you call oldMethod console.warn will spit out the line number of the wherever in your framework you have the

    var deprecated = function(fn) {
       console.warn("Deprecated!");
       fn.apply(this, arguments);
    };
declared... but you (as the user of a framework) still have no real idea where the actual function you shouldn't be calling (the fn) lives in your code (especially since the function can be anonymous)... you just know that the framework is telling you you're using a deprecated function somewhere in the code you wrote.

What I really want, and what can't be achieved with your purely in-language solution (and it really is a great solution considering the constraints), is a special form for things like @deprecated such that it straight-up can capture not only the function it's decorating, but also meta-data about the function (e.g. where it's declared, the caller / callee, etc.)


I was responding to the person responding to you mostly, but thanks for clarifying, I'd not taken your issue seriously in my reply.

Having just a deprecated macro seems very narrow and special case to me.

Something like FF's getSource would work.

    var deprecated = function(fn) {
       console.warn("Function "+fn.name+" deprecated (at "+fn.getSource()+")");
       fn.apply(this, arguments);
    };
but obv. isn't useful in real code at the moment because it is FF-only (afaik).

I'm generally quite nervous of 'special forms' in languages that don't allow you to define them yourself. Part of why scheme is so powerful is that most of it can be implemented in the same tools it gives you. I'd like to see that approach.


That's what a debugger (or a stack trace) is for. Who debugs just by line numbers anymore?




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

Search: