Skip to content

Function stubs? #3093

@00dani

Description

@00dani

In MoonScript, it's possible to get a reference to an object method that remains bound to its original object. This feature is called a "function stub" and works like this:

higherOrderFunction obj\method
-- compiles to
return higherOrderFunction((function()
  local _base_0 = obj
  local _fn_0 = _base_0.method
  return function(...)
    return _fn_0(_base_0, ...)
  end
end)())

Given the way JavaScript's objects work and that Function::bind isn't reliably available on all browsers, a similar functionality in CoffeeScript would be valuable.

We don't use \ to denote method calls in CoffeeScript, so simply copying MoonScript's notation for the concept wouldn't work so well; perhaps reusing the bound-function symbol => in the contexts we use . would, like so?

higherOrderFunction obj=>method

Would compile to:

higherOrderFunction(function () {
  return obj.method.apply(obj, arguments);
});

The above CoffeeScript is currently a syntax error, so adding this use for => shouldn't break backwards-compat with anything. I'm open to other operator suggestions, of course.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions