Skip to content

Losing imported module in functions when adding functions to a class using R6$set #382

Answered by klmr
dereckmezquita asked this question in Q&A
Discussion options

You must be logged in to vote

R6 seems to reassign it to the object’s private environment?

That’s exactly right. In my opinion this is a seriously questionable design decision of R6, but here we are.

My suggestion would be not to attach named functions to R6 classes. Instead, you can dispatch via an anonymous function, e.g.:

MyClass$set("private", ".validate_color", \(c) validators$.validate_color(c))

That said, your solution works as well. The reason it works is that importing ./constants makes the name constants available in the module environment where MyClass is defined. With that, MyClass$.validate_color will be able to find that name, since the module environment is an enclosing (/parent) environment of the My…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@dereckmezquita
Comment options

Answer selected by dereckmezquita
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants