Navigation Menu

Skip to content
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

The function keyword #758

Closed
zmthy opened this issue Oct 11, 2010 · 6 comments
Closed

The function keyword #758

zmthy opened this issue Oct 11, 2010 · 6 comments

Comments

@zmthy
Copy link
Collaborator

zmthy commented Oct 11, 2010

Now that we have a format for allowing named functions without breaking IE, it would be great for all functions to have this format. The format involves defining and calling another function to create a closure though, so it's not really realistic to use this for every function.

As such, I would like to propose introducing the function keyword, which is obviously already excluded as a JS keyword from CS. Use of the keyword names the function directly above it with the name of the variable it is assigned to (anon if there isn't one), and then compiles any use of function inside of it to the name of that function.

((count) -> function count + 1 if count < 5) 0

becomes:

(function anon(count) {
  return count < 5 ? anon(count + 1) : undefined;
})(0);

It's a great way to refer to the function above if you don't need to assign it to a variable or if you're not sure about the security of said variable. It can also just be used to name a function if you want to use it for reflection.

I've already done most of the work towards implementing its use, and I'm curious to see what you think of it.

@jashkenas
Copy link
Owner

You shouldn't have to worry about the security of a local variable. Is there a situation where this would work, and referencing a local variable wouldn't?

@zmthy
Copy link
Collaborator Author

zmthy commented Oct 11, 2010

I was thinking more about when it's just annoying to have to make that assignment. For example (quite contrived but also very plausible):

one =
  two:
    three:
      four: -> function

If we export one (or any of those intermediate objects) we can't know for sure that one.two.three.four refers to the function. Sure, we could throw in this:

four: four = -> function

but it's annoying and leaves you with this weird local variable floating around just so you can safely refer to the function.

@jashkenas
Copy link
Owner

In my opinion, it's nicer to refer to a meaningfully-named local variable than it is to a generic "function".

If this is more about named functions than recursive forms, we can certainly find a way to compile named functions for IE -- it just takes a little more doing. For example, functions assigned as object properties can safely be named, because the variables don't clash. Functions with names that do clash can be assigned with slightly altered names. Functions that are assigned to local variables can be declared with that name, instead of assigned...

@zmthy
Copy link
Collaborator Author

zmthy commented Oct 11, 2010

Really? As I understand it (I don't have anything to test it on), IE creates the named function as a definition, meaning it eats every variable in the same scope regardless of where it's actually defined.

var g = 10, obj = { fn: function g() {} };
puts(g); # Function, not 10.

In order to make it work properly for everything, we'd need to apply the fix to everything that isn't a declaration.

And I personally feel a lot better about being able to safely refer to the function without the use of a variable. Plus if I rename variables on a whim, which I tend to do fairly often, there's less refactoring to do.

@hen-x
Copy link

hen-x commented Oct 11, 2010

Under this proposal, would it be possible to compile function directly to arguments.callee if no name is specified?

@jashkenas
Copy link
Owner

arguments.callee is a fine way to accomplish this (if you're not doing ECMA5 strict). I'm afraid I'm going to close this ticket though. We just found out that the original named functions patch doesn't work in IE at all. You get errors like "Literal.super.constructor isn't an object". So I'm reverting named class functions as well...

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants