r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
405 stars 56 forks source link

Get rid of trailing commas in class definition #98

Closed krlmlr closed 7 years ago

krlmlr commented 7 years ago

Would you consider also supporting the following notation:

R6::R6Class(
  "Test",

  public = ~{
    initialize = function(...) NULL

    run = function(...) NULL
  },

  private = ~{
    member <- NULL
  }
)

The formula objects created here need some massaging:

x <- ~{a = 1; b = Sys.sleep(10); c = 3}
expression_list <- as.list(x[[2]])[-1]
operators <- lapply(expression_list, "[[", 1) # should all be `=`
names <- lapply(expression_list, "[[", 2) # name objects
calls <- lapply(expression_list, "[[", 3) # calls
wch commented 7 years ago

I agree that dealing with trailing commas can be a bit annoying, but I'm not convinced that it justifies adding support for a new syntax, which would be kind of magical for those who haven't seen it before.

You could also write your own function like this:

listify <- function(expr) {
  expr <- substitute(expr)
  env <- new.env(parent = parent.frame())

  eval(expr, envir = env)
  as.list.environment(env)  
}

listify({
  initialize = function(...) NULL
  run = function(...) NULL
})
# $initialize
# function (...) 
# NULL
# <environment: 0x6e28610>
# 
# $run
# function (...) 
# NULL
# <environment: 0x6e28610>