Skip to content

unbound functions with this as a default parameter value #2362

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

Closed
michaelficarra opened this issue May 30, 2012 · 6 comments
Closed

unbound functions with this as a default parameter value #2362

michaelficarra opened this issue May 30, 2012 · 6 comments
Labels

Comments

@michaelficarra
Copy link
Collaborator

This may be a duplicate, but I can't find it:

$ coffee -bep 'do (a=@) => a'
var _this = this;

(function(a) {
  return a;
})(this);
$ coffee -bep 'do (a=@) -> a'
(function(a) {
  return a;
})(this);
$ coffee -bep '((a) => a)(@)'
var _this = this;

(function(a) {
  return a;
})(this);
$ coffee -bep '((a) -> a)(@)'
(function(a) {
  return a;
})(this);
$ coffee -bep '((a=@) => a)()'
var _this = this;

(function(a) {
  if (a == null) {
    a = _this;
  }
  return a;
})();
$ coffee -bep '((a=@) -> a)()'
(function(a) {
  if (a == null) {
    a = this;
  }
  return a;
})();
$ 

Notice that the last compilation has different behaviour from the rest, though one would expect it to have equivalent behaviour. I suspect there's similar problems with arguments, regardless of boundedness.

@michaelficarra
Copy link
Collaborator Author

Now that I think about it, this could actually be a desirable output, though syntactically confusing (this being outside the function body). I can see how someone might want a parameter to default to the context of the function.

The problem with arguments remains regardless.

@satyr
Copy link
Collaborator

satyr commented May 31, 2012

Due to the specialcasing, do -> (or do [] = -> etc.) and do (->) (or do [] = [] = -> etc.) are different wrt the scope/type of default arguments. One of the reasons Coco introduced let in place of do ->.

@satyr
Copy link
Collaborator

satyr commented Jun 2, 2012

Besides, do (a) -> and do ((a) ->) is different by definition since #960. This behavior is more like feature than bug.

@vendethiel
Copy link
Collaborator

Seems expected to me

@xixixao
Copy link
Contributor

xixixao commented Dec 17, 2013

This is tagged bug, what is the desired output?

@michaelficarra
Copy link
Collaborator Author

I think we've come to the conclusion that default arguments are evaluated in the context of the function body, so the current output is desired. Closing as invalid.

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

No branches or pull requests

4 participants