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

locally defined functions that call themselves #41

Open
clarkfitzg opened this issue Jul 18, 2019 · 2 comments
Open

locally defined functions that call themselves #41

clarkfitzg opened this issue Jul 18, 2019 · 2 comments

Comments

@clarkfitzg
Copy link
Contributor

If a function is defined locally and calls itself (so it's recursive), then ideally CodeDepends should identify that the usage of the function is local, and not from a package. This came up for me in tryCatch, specifically getInputs(tryCatch)[[3L]] where tryCatchList defines and uses itself. One can use Recall to achieve the same result.

Here's a minimal example:

add_123 = function(x){
    add = function(y, ...){
        if(!missing(y)){
            y + add(...)
            #y + Recall(...)  # Alternatively
        } else 0
    }
    add(x, 1, 2, 3)
}

info = getInputs(add_123)

info[[3]]@functions["add"]  # FALSE, should be TRUE

info[[4]]@functions["add"]  # TRUE, as expected

I can work around this behavior by checking if the name of the function used is also an output.

The documentation for the functions slot from getInputs states:

Note that this is not recursive.

I understand this to mean that it doesn't recursively look at all functions called.

@duncantl
Copy link
Owner

For the straightforward cases, we can add that. I just didn't look for function definitions explicitly.
Not certain yet where I want to do this without giving it a little thought, i.e. in CodeAnalysis or in CodeDepends. I'll try to find time to rationalize the separation and role of each of these in the next few weeks.

@clarkfitzg
Copy link
Contributor Author

There's also a related issue with TryCatch. That is, tryCatchList uses tryCatchOne that's only defined later in the body. I'm mentioning it here so we don't forget.

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

No branches or pull requests

2 participants