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

Operators as Functions vs. this #805

Closed
summivox opened this issue Jan 6, 2016 · 4 comments
Closed

Operators as Functions vs. this #805

summivox opened this issue Jan 6, 2016 · 4 comments

Comments

@summivox
Copy link
Contributor

summivox commented Jan 6, 2016

o = a: 1, b: 2, f: -> <[a b]> .map (@.)
o.f!

currently compiles to:

var o;
o = {a: 1,b: 2, f: function(){
  return ['a', 'b'].map(function(it){
    return this[it];
  });
}};
o.f();

I believe a more useful semantics would be to implicitly bind this, as if I wrote:

o = a: 1, b: 2, f: -> <[a b]> .map ~> @[it]
o.f!

which compiles to:

var o;
o = {a: 1,b: 2, f: function(){
  var this$ = this;
  return ['a', 'b'].map(function(it){
    return this$[it];
  });
}};
o.f();

IIRC there are other situations where some expression has this implicit this binding semantics -- it felt so natural I forgot which ;)

@summivox
Copy link
Contributor Author

summivox commented Jan 6, 2016

Ah I recalled. A list comprehension has implicit this binding.

a = [this.f .. for arr] .filter g

compiles to:

var a;
a = (function(){
  var i$, x$, ref$, len$, results$ = [];
  for (i$ = 0, len$ = (ref$ = arr).length; i$ < len$; ++i$) {
    x$ = ref$[i$];
    results$.push(this.f(x$));
  }
  return results$;
}.call(this)).filter(g);

@vendethiel
Copy link
Contributor

That's not really the same, because the closure we're generating is really an implementation detail.

@summivox
Copy link
Contributor Author

summivox commented Jan 6, 2016

That's really not the same

True.

Back on operators as functions: I can imagine code relying on unbound this, so a breaking change would likely be impossible. Alternatively, is it possible to have a bound version?

@vendethiel
Copy link
Contributor

Let's move this to #634 :-).

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

2 participants