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

is_calleable assertion #160

Open
devurandom opened this issue Jan 6, 2019 · 10 comments
Open

is_calleable assertion #160

devurandom opened this issue Jan 6, 2019 · 10 comments

Comments

@devurandom
Copy link

It would be useful to have an is_calleable assertion that tests whether is_function(argument) or is_function(debug.getmetatable(argument)._call).

@Tieske
Copy link
Member

Tieske commented Jan 8, 2019

this should be easy to add, see http://stevedonovan.github.io/Penlight/api/libraries/pl.types.html#is_callable

care to create a PR @devurandom ?

@devurandom
Copy link
Author

You mean to copy the code from Penlight?

--- is the object either a function or a callable object?.
-- @param obj Object to check.
function types.is_callable (obj)
    return type(obj) == 'function' or getmetatable(obj) and getmetatable(obj).__call and true
end

Sure, I can do that.

@Tieske
Copy link
Member

Tieske commented Jan 8, 2019

No, busted already relies on Penlight, so all you have to do is add an assertion that uses that function.

@devurandom
Copy link
Author

devurandom commented Jan 8, 2019

But the assertions reside in luassert/src/assertions.lua, i.e. in luassert, which currently does not depend on Penlight.

@Tieske
Copy link
Member

Tieske commented Jan 8, 2019

@devurandom you are totally right, sorry for the noise.

@DorianGray
Copy link
Contributor

Feel free to make a PR and reference this issue

@Tieske
Copy link
Member

Tieske commented Sep 13, 2020

luassert has its own implementation btw; in luassert.util, see https://github.com/Olivine-Labs/luassert/blob/master/src/util.lua#L274

@tmillr
Copy link
Contributor

tmillr commented Sep 15, 2024

luassert has its own implementation btw; in luassert.util, see https://github.com/Olivine-Labs/luassert/blob/master/src/util.lua#L274

Technically, that impl is incorrect. See here: neovim/neovim#29536

The Penlight impl is also incorrect.

@Tieske
Copy link
Member

Tieske commented Sep 17, 2024

wouldn't an object with metatable that has another metatable with a __call method be callable? iirc those lookups are chained.

@tmillr
Copy link
Contributor

tmillr commented Sep 18, 2024

wouldn't an object with metatable that has another metatable with a __call method be callable? iirc those lookups are chained.

Not for Lua 5.1 and Luajit. Maybe this has changed since 5.1 though, not sure.

You can see the tests here (neovim is pinned to 5.1): https://github.com/neovim/neovim/pull/29536/files

neovim/neovim#27457 (comment)

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

No branches or pull requests

4 participants