Git Product home page Git Product logo

Comments (5)

lionel- avatar lionel- commented on May 26, 2024 1

In the meantime you can wrap the generators like this @dfalbel

R6::R6Class(
  "generator",
  private = list(
    generators = list(x = coro::generator(function() {
      for (i in 1:10)
        yield(i)
    }))
  ),
  public = list(
    x = function() private$generators$x()
  )
)

from coro.

lionel- avatar lionel- commented on May 26, 2024 1

Thanks for your input @wch!
I've filed an issue for I() at r-lib/R6#222.

Closing this since it's not a coro issue.

from coro.

lionel- avatar lionel- commented on May 26, 2024

Maybe there should be a way of preventing R6 from treating a function as a method? For example functions wrapped in I(). And maybe functions with a custom class should automatically get that treatment?

Relatedly, maybe R6Class() should issue a warning when methods don't inherit from the current environment? Changing their environments in the background seems a bit unsafe.

What do you think @wch?

from coro.

dfalbel avatar dfalbel commented on May 26, 2024

Thanks! That works great for me!

from coro.

wch avatar wch commented on May 26, 2024

With R6Class, all functions that are part of public or private are assumed to be methods, and their environment is modified when the class is instantiated.

R6 does have the notion of members function that are not methods, though. They must be assigned either in initialize() or after the object is created. So you could so something like this:

num_generator <- coro::generator(function() {
  for (i in 1:10)
    yield(i)
})

Generator <- R6::R6Class(
  "Generator",
  public = list(
    initialize = function() {
      self$x = num_generator
    },
    x = NULL
  )
)

(You could also call coro::generator(function() ....) inside of initialize(), but that would create a new factory for each instance, which is less efficient.)

The difference between methods and non-method functions is recognized when the object is cloned; in the cloned object, methods will have their environment changed (so they can find the correct self and private), but non-method functions will not have their environment changed.

@lionel-, the idea of using I() to mark non-method functions is interesting. Want to file an issue on R6 so it can be discussed there? Or maybe the current behavior just needs to be better documented.

from coro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.