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

[R-package] small simplifications in Booster and Predictor #4858

Closed
wants to merge 1 commit into from

Conversation

jameslamb
Copy link
Collaborator

Working through #4857, I noticed a few functions in the R package doing something like this:

some_function <- function(x = NULL) {
    if (is.null(x)) {
        x <- -1L
    }
}

This PR proposes simplifying that code by just changing the default values in function signatures.

some_function <- function(x = -1L){
}

Notes for Reviewers

Function defaults are lazily evaluated in R, so no memory is saved by using NULL instead of an integer.

# this does not fail until the function is called
do_something <- function(m = stop("hey")){
     return TRUE
}

Function defaults are not stored in the global environment, so it isn't necessary to worry about side effects like you have to with using mutable types as function defaults in Python.

do_something <- function(x = 10L) {
    x <- x + 1L
    print(x)
}

do_something()
# 11

do_something()
# 11

do_something()
# 11

Copy link
Collaborator

@StrikerRUS StrikerRUS left a comment

Choose a reason for hiding this comment

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

LGTM!
Are these functions in public API? If so, this PR should be marked as breaking because it drops possibility of using NULL at a user side.

@jameslamb
Copy link
Collaborator Author

jameslamb commented Dec 6, 2021

Are these functions in public API? If so, this PR should be marked as breaking because it drops possibility of using NULL at a user side.

Ah good point! I was so focused on this being the mechanism for the default, I didn't think about users actually passing NULL explicitly.

I do consider these methods public, see #4685 (comment) for an explanation.

Given that, I don't support these changes any more 😬 . I don't think this very small simplification is worth breaking users' code. I'm going to close this.

@jameslamb jameslamb closed this Dec 6, 2021
@jameslamb jameslamb deleted the simplify-r branch December 6, 2021 03:37
@StrikerRUS
Copy link
Collaborator

OK, no problem! 🙂

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants