yutannihilation / savvy

A simple R extension interface using Rust
https://yutannihilation.github.io/savvy/guide/
MIT License
63 stars 3 forks source link

Lock enviroment and bindings? #261

Closed eitsupi closed 3 months ago

eitsupi commented 3 months ago

Currently, objects generated from Structs and functions generated from methods of that Struct are not locked, so they may be overwritten by accident. I suppose it's okay to lock these in the wrapper file. For example:

`.savvy_wrap_FooWithDefault` <- function(ptr) {
  e <- new.env(parent = emptyenv())
  e$.ptr <- ptr
  lockBinding(".ptr", e)
  e$`default_value_method` <- `FooWithDefault_default_value_method`(ptr)
  lockBinding("default_value_method", e)

  class(e) <- "FooWithDefault"
  lockEnvironment(e)
  e
}
shikokuchuo commented 3 months ago

Can't be certain what you want to achieve here, but in nanonext I simply define the methods for [[<- and $<- as no-ops for the relevant class.

This avoids these cumbersome operations every time, and still allows modification using assign() in case you actually want to do that in other parts of your code.

yutannihilation commented 3 months ago

they may be overwritten by accident.

What accident did you experience?

I simply define the methods for [[<- and $<- as no-ops for the relevant class.

Thanks for the idea, this sounds simple.

eitsupi commented 3 months ago

What accident did you experience?

I haven't had any real trouble, but I imagine that there might be a case where you might want to allow updates via $ in a dataframe-like object, just as the arrow Table built on R6 allows column updates via $. In that case I figured something like .ptr would need to be prevented from being overwritten.

yutannihilation commented 3 months ago

I see, thanks for the details.