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

Allow class-level members #226

Closed
iqis opened this issue Jan 18, 2021 · 2 comments
Closed

Allow class-level members #226

iqis opened this issue Jan 18, 2021 · 2 comments

Comments

@iqis
Copy link

iqis commented Jan 18, 2021

Some OO designs may make use of a class-level member.
For example, incrementing a serial number:

Employee <- R6::R6Class("Employee", 
                        class_member = list(count = 0), 
                        public = list(
                            initialize = function(name){
                                self$name <- name
                                self$class$class_member$count <- 
                                    self$id <- 
                                    self$class$count + 1
                            }, 
                            id = NULL
                            name = NULL
                        ))
john <- Employee$new("John")
mary <- Employee$new("Mary")

john$id
# [1] 1
mary$id
# [1] 2

Depends on #225.

@wch
Copy link
Member

wch commented Jan 19, 2021

This sounds similar to having Java-style static members: #222 (comment)

@iqis
Copy link
Author

iqis commented Jan 19, 2021

Thanks, I think this is an exact duplicate... Wasn't using static when searching for existing issues.

@iqis iqis closed this as completed Jan 19, 2021
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