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

R6 doesn't like initialize() to use a parameter named like the class #195

Closed
moodymudskipper opened this issue Nov 15, 2019 · 1 comment
Closed

Comments

@moodymudskipper
Copy link

This was a bit of a pain to debug :

foo <- R6::R6Class("foo",
                   public = list(
                     foo = NULL,
                     initialize  = function(foo){}
                   ))
foo$new()
#> <foo>
#>   Public:
#>     clone: function (deep = FALSE) 
#>     foo: NULL
#>     initialize: function (foo)

adding body containing foo in initialize, I get a cryptic message

foo <- R6::R6Class("foo",
                   public = list(
                     foo = NULL,
                     initialize  = function(foo){foo}
                   ))
foo$new()
#> Error in .subset2(public_bind_env, "initialize")(...): argument "foo" is missing, with no default

Created on 2019-11-15 by the reprex package (v0.3.0)

I would suggest a more helpful error message, and maybe some documentation about this behavior (I didn't find any in the doc)

@wch
Copy link
Member

wch commented Nov 18, 2019

I've made a change in 5cd8acd so that it now says:

Error in initialize(...) : argument "foo" is missing, with no default

Note that the cause of the error is the function (initialize) tries to use the foo argument, but you're calling initialize without providing a value for foo. You'll get that same error message if you call any R function that way, not just something from R6.

@wch wch closed this as completed Nov 18, 2019
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