r-lib / R6

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

Feature Request: Setting public methods in initializer #102

Closed ericreg closed 7 years ago

ericreg commented 7 years ago

It would be great if one could set a public method in the initializer. This can currently be forced by calling unlockBinding(sym, self) For example:

class.Path <- R6Class("Path",
    public = list(
    initialize = function(path = NA) {
        t = seq(0, 1, length.out = nrow(path))
        smooth.x = smooth.spline(t, path$X)
        smooth.y = smooth.spline(t, path$Y)
        smooth.z = smooth.spline(t, path$Z)

        unlockBinding("approximator", self)
        self$approximator <<- function(t, deriv = 0) {

            return(data.frame(
            x = predict(smooth.x, t, deriv = deriv)$y,
            y = predict(smooth.y, t, deriv = deriv)$y,
            z = predict(smooth.z, t, deriv = deriv)$y));
        };
        lockBinding("approximator", self)
    },
    approximator = function() {  }
    ),
    private =
    list(
    ))

I think this feature should be available. Given that R is a functional language, being able to set functions in the constructor.

ericreg commented 7 years ago

Nevermind. This is possible by setting approximator = NULL. Ignore.