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

Safe inspect version #46

Closed
wants to merge 2 commits into from
Closed

Conversation

zugzug90
Copy link

@zugzug90 zugzug90 commented Oct 15, 2019

For cases when you need to print out whatsoever results of some function call you can use another version of inspect function: safe_inspect. Usage scenario example below:

inspect = require("inspect")

-- Returns 2 values: one is a Lua table, another - just a primitive type - the number
function two_values()
    return {x = 15, y = 6}, 2
end

print(inspect.safe_inspect(two_values()))

Result will be:

{
  x = 15,
  y = 6
}

This allows to prevent primitive type handling errors when using original inspect function by just throwing away all the rest returned values if they are not of the table type otherwise using 2nd arg as parameters map like inspect function does.

See issue: #39

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.6%) to 97.706% when pulling 6ba0d91 on zugzug90:safe-inspect into b611db6 on kikito:master.

@@ -218,6 +218,30 @@ Gotchas / Warnings

This method is *not* appropriate for saving/restoring tables. It is meant to be used by the programmer mainly while debugging a program.

Safe inspect version
====================
For cases when you need to print out whatsoever results of some function call you can use another version of `inspect` function: `safe_inspect`. Usage scenario example below:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean "whatever", not "whatsoever".

@justinmk
Copy link

justinmk commented Oct 17, 2019

"safe" isn't really the right term here. And this seems like a very specific function for a very specific case.

#39 can be addressed simply by wrapping the input in a table parentheses, e.g.:

print(inspect.inspect((two_values())))

It doesn't make sense to have an entirely new function instead of that.

@kikito
Copy link
Owner

kikito commented Sep 23, 2021

Use a table to wrap all the possible values returned by the function. Instead of:

print(inspect(two_values())

Do this:

print(inspect({two_values()})

You can usually drop the two parenthesis of the call and just leave the brackets. This is equivalent to the previous one:

print(inspect{two_values()})

So inspect(call()) is "unsafe", but inspect{call()} is "safe"

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

Successfully merging this pull request may close these issues.

None yet

4 participants