jonthegeek / factory

factory: Build Function Factories
Other
49 stars 6 forks source link

Arguments as parameters #29

Open TylerGrantSmith opened 4 years ago

TylerGrantSmith commented 4 years ago

This would probably take some special logic to make work:


factory_fun <- factory::build_factory(fun = function() {
  a <- c(a=2)
  a$a
}, a)
factory_fun(rlang::sym("b"))
#> function () 
#> {
#>     b <- c(a = 2)
#>     b$b
#> }

Created on 2020-06-03 by the reprex package (v0.3.0)

jonthegeek commented 4 years ago

Is this the desired output?

function ()
{
  b <- c(b = 2)
  b$b
}

I'm not certain how this should work, so I'm not sure yet if it's fixable. Do you have a less contrived example?

TylerGrantSmith commented 4 years ago

Sure...you could just have the body be c(b = 2). I'm not sure that it is more or less contrived though.

jonthegeek commented 4 years ago

I'm honestly a little surprised that this doesn't "work" already; the factory builder is pretty zealous about finding instances of the parameter (it'll do so inside of quotes, for example).

Actually, on that note... this fails in a different way, which might be a clue:

factory_fun <- build_factory(
  fun = function() {
    a <- double()
    a[["a"]] <- 2
    a[["a"]]
  }, 
  a
)
factory_fun(rlang::sym("b"))

This is the function it produces:

function () 
{
    b <- double()
    b[[b]] <- 2
    b[[b]]
}

Note that the quotes around "b" have been removed. I'm somewhat shocked that this happens, and that feels worth digging into. I think I want to ignore stuff inside "" in general, but I'm not certain yet.