jtarchie/underscore-lua

_.invoke with self parameter

vertti opened this issue · 6 comments

I'd like something like this to work:

__.invoke(props, "setVisible", visible)

where props is a table and for each element in that table I'd like an invoke like

 prop:setVisible(visible)

in other words that self could be passed as the first argument to the function.

Not a bad idea, but I'd hate to change the entire expectation of invoke to just support self. Would a method invokeSelf be worth it?

That's fine with me, I seem to run into this use case often. invokeWithSelf might be a better name as it doesn't invoke self but does an invoke and supplies self as parameter.

You know it turns out, that invoke already supported this. The function being called has to either have self as the first argument or be defined via the colon syntax.

The following passes in the test suite:

  local object = {count=0}
  function object:incr()
    self.count = self.count + 1
    return self.count
  end

  it("passes the object to the method that is being invoked", function()
    local array = {object, object, object}
    assert.same(_.invoke(array, 'incr'), {1,2,3})
  end)

Did it not work for you when trying _.invoke(props, "setVisible", visible)?

@vertti were you able to test the above with your code?

@vertti any feedback? Otherwise I am closing the ticket.

Yes, I don't know what I originally did wrong but invoke seems to work fine with self. Thanks.