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

Anonymous function reference #2671

Closed
mehcode opened this issue Jan 16, 2013 · 11 comments
Closed

Anonymous function reference #2671

mehcode opened this issue Jan 16, 2013 · 11 comments

Comments

@mehcode
Copy link

mehcode commented Jan 16, 2013

From #2665

I vote for a way to name or call the current function so we can have full anonymous function recursion.

Something like a re-purposing of the function keyword to mean the name of the current function:

fib = (n) ->
  switch n
    when 0, 1 then n
    else function(n - 1) + function(n - 2)
@satyr
Copy link
Collaborator

satyr commented Jan 16, 2013

#758

@satyr satyr closed this as completed Jan 16, 2013
@michaelficarra
Copy link
Collaborator

@satyr: That's pretty ancient. Maybe it's worth reconsidering?

@mehcode
Copy link
Author

mehcode commented Jan 16, 2013

It could be optimized so that the function is only named and put in its own invoked closure if the function keyword is used, etc.

The reason that @jashkenas closed #758 (in my opinion) is the weirdness with naming functions in IE. That issue also allowed for the developer to force a function to be named for no reason. This doesn't allow that. It only allows for functions to be named if the function keyword is used inside the function (which I admit is close, but slightly different).


Also on a slightly more controversial note. Even jQuery is abandoning IE support soon. We should at least have a compiler switch to do so (in redux). This switch could even enable or disable certain features, such as this one.

@satyr
Copy link
Collaborator

satyr commented Jan 16, 2013

It's a duplicate anyway. Reopen #758 if you dare BDFL.

Personally I too wanted an arguments.callee alternative, but didn't like function for it; it reads too weird.

The reason that @jashkenas closed #758 (in my opinion) is the weirdness with naming functions in IE

More like he didn't feel the need for the proposal, or like it at all:

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

@vendethiel
Copy link
Collaborator

Maybe labels for closures à la coco ?

@satyr what does bdfl stands for ?

@mehcode
Copy link
Author

mehcode commented Jan 16, 2013

@vendethiel
Copy link
Collaborator

Okay so I got that right but without context :p.

@epidemian
Copy link
Contributor

I'm not sure what problem this is supposed to solve. In cases where the anonymous function does not have a variable to reference it or is too nested inside an object so safely reference it:

very.nested.obj =
  countdown: (n) ->
    # I want to call `countdown` here, but i don't want to call it 
    # very.nested.obj.countdown!

We can now either assign that function to a meaningful variable:

very.nested.obj =
  countdown: countdown = (n) ->
    if n is 0 then 'zero was reached' else countdown n - 1

Or, if we don't want the variable name to leak outside its scope, we can use a do block:

very.nested.obj =
  countdown: do -> 
    countdown = (n) ->
      if n is 0 then 'zero was reached' else countdown n - 1

Yes, kind of a noisy definition there, i personally would just use a normal variable. In any case, i think it's preferable to be explicit and name the function if we want to call it recursively than adding a new language construct just for these (arguably strange) cases.

@jashkenas
Copy link
Owner

Sorry to be dense, but I don't understand why you'd prefer to write:

fib = (n) ->
  switch n
    when 0, 1 then n
    else function(n - 1) + function(n - 2)

Instead of:

fib = (n) ->
  switch n
    when 0, 1 then n
    else fib(n - 1) + fib(n - 2)

... in this case. Much clearer without the special single-serving magic keyword.

@satyr
Copy link
Collaborator

satyr commented Jan 16, 2013

why you'd prefer to write:

For micro-DRY I guess, the same reason we prefer x += 1 in place of x = x + 1. E.g. former fib needs two less modifications when renaming.

@michaelficarra
Copy link
Collaborator

@satyr: And of course when there's no direct reference, as in @epidemian's example above. Using the long chain of member accesses, as you know, has different semantics than directly referencing a function by name. The IIFE solution is elegant but verbose.

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

6 participants