-
Notifications
You must be signed in to change notification settings - Fork 2k
inserting variable into the current lexical scope #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Well, that'd really be feasible in coffeescript. We could have a global operator that inject the variables of an object into the local scope. If people show an interest, I'd be happy to produce a patch for that. |
So how might you rewrite the example above? |
We could have a keyword, maybe something like
Which would imply you'd be able to write your example as :
|
matehat: I'd be interested in seeing your implementation as well. Tim_Smart and I have talked a lot about sandboxing in JS and the dynamic creation of local variables, but outside of If we could have dynamic local variables, it would greatly improve the |
Hmm, maybe I'm seeing to simple then. I might be missing something obvious :P |
That might well be quite useful . It does use "eval" - but that doesn't really matter here. |
This is basically the same thing that |
that's the idea. Anyway, we had a talk on |
I'd be interested to know what the performance issues were ? As far as I knew - the eval would occur just once. |
It would for the usual case, but if it is in the core, it's kind of as if we proposed it as a good option in general, even in recurring or iterative situations. After some benchmarking, the ratio in performance between normal and A thing that could make us overcome this is if the syntax were more precise on what we want to use from an object :
That would not require us to use the |
Yes that's kinda cool. But I suppose it's not much terser than a: vars.a; b: vars.b ? O = {a:1,b:2,c:function() {} } for(var i=0; i< 100000; i++) { var a=O.a, b=O.b, b=O.c; } 244 ms 0.024ms per iteration for(var i=0; i< 100000; i++) { eval("var a=O.a, b=O.b, b=O.c;") } 570ms 0.057ms per iteration Which is only twice as slow ? Given that a "using" statement wouldn't typically be called that often compared with looped code, I don't think that's signifcantly slow ? |
What I thought would be nice in explicit injection like the above, is that it could offer the Sure, it does not look so much terser, but injecting 5 variables in your alternative does get kinda awkward. A direct application of this whole issue is module sandboxing and in that case, we don't always need 20 names or so to be imported and such a syntax would still look clean with 5-7 names injection. |
In your example, I get 65k ops/sec for vanilla and 471 for eval'ed one .. so that's more a factor of around 140 times faster for vanilla. |
Funny we get different amounts of performance - what browser are you running on (mine is FF 3.6 - so it probably ) ? Point taken on the We should also get a better handle on whether it really would be a performance issue - it really depends on use. It's not relative speed compared with naked assignment that's important here - but time taken relative to the rest of the functionality of the program. |
Here's a nice example of where this is useful for cleaning up, using matehat's suggestion: (-> MyClass: { map: (object, callback) -> items: [] each(object, (i) -> items.push(callback.apply(@, arguments)) items each: (object, callback) -> for v,i in object callback(i) } use MyClass * # or use MyClass each, map # export @MyClass: MyClass )() As you can see it mixes in the module's method's to itself, meaning it can refer to them simply by itself without the MyClass prefix. This example was adapted from : http://dailyjs.com/2010/03/19/private-vars-scope/ |
With Node.js deprecating
As an alternative syntax, it would be nice to provide a way to copy properties onto another object:
It shouldn't be too bad and as long as we are aware of the actual process and implementation. |
Interesting Stan, but I think that extending I'm working on an extension that uses eval for I think that gives enough flexibility either way. |
I hope this helps. |
The second example can be written: use MyClass map, first --> var map = MyClass.map, first = MyClass.first which neatly avoids the eval. |
I personally think the two could be used at once. We could write:
That way, only one case needs |
ok - I made an extension that handles this : http://gist.github.com/348943 !! Syntax is: using MyModule * using MyModule get, set, del I left out the 'on this' type syntax for now. The issue I have with it is that while the other two methods affect local scope and so are somewhat contained, adding methods to 'this' is a bit more trixy and could cause problems elsewhere. If you really wanted to do this, you could do this using something like edit: it seems to be able to compile each line ok, but blows up if it's used in multiple lines. I think it might be something to do with |
Good job weepy. About the For the problem you are experiencing, you're splitting by single spaces the rest of all content (with the |
Ah thanks matehat - I've updated the gist to fix that. I see where you're coming from on the 'extends' functionality. We'd need to get the syntax right - so that it's clear that the object methods are being mixed into the current scope ? include MyModule * include MyModule get, set include MyModule * on OtherObj Any other ideas ? |
To get enough attention, you should reopen the ticket, though. Being closed, we're the only ones reading it ;) |
Prolly good idea to extend the regex:
? |
I'm getting the updates too. It's really neat that you've made an extension for it, but we've talked about it a couple of times in |
Well I think it's misguided to be concerned about |
I've started working on a macros branch and the test file implements a simple
|
defo useful functionality - i use something similar in my JS all the time. Trouble with the keywords I wanted to ask - what is the advantage of your macro implementation over the normal CoffeeScript.extend ? They appear to server similar functions ? |
The current macro can be renamed to More in issue 313. |
They look pretty interesting :-D Nemerle is another language that seems to use macros extensively: http://nemerle.org/Macros |
I think there might not be a way round this. I'm trying to insert variables from another scope into the current scope as local variables like so:
Obviously this doesn't work since "eval" is outside the closure. But there doesn't seem to be a way to reference the current lexical scope. Even mixin.call(this) wouldn't work.
As I said above, I'm not sure there's an easy round this - even for Coffee, but I'd be interested in all your ideas.
Cheers
J
The text was updated successfully, but these errors were encountered: