The example is invoking NextMethod via an anonymous function, which is not allowed (see documentation for NextMethod). Normally one gets a runtime error "'NextMethod' called from an anonymous function", but not here as the anonymous function is called via do.call. I will fix so that there is a runtime error in this case as well, thanks for uncovering this problem. I don't think there is a way to replace (unnamed) arguments in dots for NextMethod. Tomas On 02/21/2018 02:16 PM, I?aki ?car wrote:> I've set up a repo with a reproducible example of the issue described > in my last email: > > https://github.com/Enchufa2/dispatchS3dots > > I?aki > > 2018-02-20 19:33 GMT+01:00 I?aki ?car <i.ucar86 at gmail.com>: >> Hi all, >> >> Not sure if this belongs to R-devel or R-package-devel. Anyways... >> >> Suppose we have objects of class c("foo", "bar"), and there are two S3 >> methods c.foo, c.bar. In c.foo, I'm trying to modify the dots and >> forward the dispatch using NextMethod without any success. This is >> what I've tried so far: >> >> c.foo <- function(..., recursive=FALSE) { >> dots <- list(...) >> # inspect and modify dots >> # ... >> do.call( >> function(..., recursive=FALSE) structure(NextMethod("c"), class="foo"), >> c(dots, recursive=recursive) >> ) >> } >> >> foobar <- 1 >> class(foobar) <- c("foo", "bar") >> c(foobar, foobar) >> Error: C stack usage 7970788 is too close to the limit >> >> There's recursion (!). But the funniest thing is that if c.foo is >> exported by one package and c.bar is exported by another one, there's >> no recursion, but c.bar is never called (!!). Why is the same code >> behaving in a different way depending on whether these functions are >> defined in the .GlobalEnv or in two attached packages? (BTW, >> isS3method() is TRUE, for c.foo and c.bar in both cases). >> >> I'm blocked here. Am I missing something? Is there a way of doing >> this? Thanks in advance. >> >> Regards, >> I?aki > >
2018-02-22 10:29 GMT+01:00 Tomas Kalibera <tomas.kalibera at gmail.com>:> > The example is invoking NextMethod via an anonymous function, which is not > allowed (see documentation for NextMethod).Thanks for your response. I definitely missed that bit.> Normally one gets a runtime > error "'NextMethod' called from an anonymous function", but not here as the > anonymous function is called via do.call. I will fix so that there is a > runtime error in this case as well, thanks for uncovering this problem.Then I did well chosing this list! Please also note that you could take that anonymous function out of the method and name it, and the behaviour would be the same. So maybe this case should issue an error too.> I don't think there is a way to replace (unnamed) arguments in dots for > NextMethod.That's a pity. IMHO, it should be some mechanism for that, but dots are special in inscrutable ways. Anyway, for anyone insterested, I found a workaround: https://github.com/Enchufa2/dispatchS3dots#workaround> > Tomas > >I?aki
On 02/22/2018 12:07 PM, I?aki ?car wrote:> 2018-02-22 10:29 GMT+01:00 Tomas Kalibera <tomas.kalibera at gmail.com>: >> The example is invoking NextMethod via an anonymous function, which is not >> allowed (see documentation for NextMethod). > Thanks for your response. I definitely missed that bit. > >> Normally one gets a runtime >> error "'NextMethod' called from an anonymous function", but not here as the >> anonymous function is called via do.call. I will fix so that there is a >> runtime error in this case as well, thanks for uncovering this problem. > Then I did well chosing this list! Please also note that you could > take that anonymous function out of the method and name it, and the > behaviour would be the same. So maybe this case should issue an error > too.I am not sure I understand how, but if you find a way to bypass the new check for an anonymous function (I intend to commit tomorrow), I will be happy to have a look if you provide a reproducible example.>> I don't think there is a way to replace (unnamed) arguments in dots for >> NextMethod. > That's a pity. IMHO, it should be some mechanism for that, but dots > are special in inscrutable ways. > > Anyway, for anyone insterested, I found a workaround: > > https://github.com/Enchufa2/dispatchS3dots#workaroundEven though technically this won't be too hard, I don't think NextMethod should be made any more complex than it is now. There should always be a way to implement special dispatch scenarios in R and your workaround shows it is possible specifically in your scenario. Tomas>> Tomas >> >> > I?aki