Kasper Daniel Hansen
2023-Jun-13 18:03 UTC
[Rd] codetools wrongly complains about lazy evaluation in S4 methods
On Sat, Jun 3, 2023 at 11:51?AM Mikael Jagan <jaganmn2 at gmail.com> wrote:> The formals of the newly generic 'qr.X' are inherited from the non-generic > function in the base namespace. Notably, the inherited default value of > formal argument 'ncol' relies on lazy evaluation: > > > formals(qr.X)[["ncol"]] > if (complete) nrow(R) else min(dim(R)) > > where 'R' must be defined in the body of any method that might evaluate > 'ncol'. >Perhaps I am misunderstanding something, but I think Mikael's expectations about the scoping rules of R are wrong. The enclosing environment of ncol is where it was _defined_ not where it is _called_ (apologies if I am messing up the computer science terminology here). This suggests to me that codetools is right. But a more extended example would be useful. Perhaps there is something special with setOldClass() which I am no aware of. Also, Bioconductor has 100s of packages with S4 where codetools works well. Kasper [[alternative HTML version deleted]]
Simon Urbanek
2023-Jun-14 00:01 UTC
[Rd] codetools wrongly complains about lazy evaluation in S4 methods
I agree that this is not an R issue, but rather user error of not defining a proper generic so the check is right. Obviously, defining a generic with implementation-specific ncol default makes no sense at all, it should only be part of the method implementation. If one was to implement the same default behavior in the generic itself (not necessarily a good idea) the default would be ncol = if (complete) nrow(qr.R(qr, TRUE)) else min(dim(qr.R(qr, TRUE))) to not rely on the internals of the implementation. Cheers, Simon> On 14/06/2023, at 6:03 AM, Kasper Daniel Hansen <kasperdanielhansen at gmail.com> wrote: > > On Sat, Jun 3, 2023 at 11:51?AM Mikael Jagan <jaganmn2 at gmail.com> wrote: > >> The formals of the newly generic 'qr.X' are inherited from the non-generic >> function in the base namespace. Notably, the inherited default value of >> formal argument 'ncol' relies on lazy evaluation: >> >>> formals(qr.X)[["ncol"]] >> if (complete) nrow(R) else min(dim(R)) >> >> where 'R' must be defined in the body of any method that might evaluate >> 'ncol'. >> > > Perhaps I am misunderstanding something, but I think Mikael's expectations > about the scoping rules of R are wrong. The enclosing environment of ncol > is where it was _defined_ not where it is _called_ (apologies if I am > messing up the computer science terminology here). > > This suggests to me that codetools is right. But a more extended example > would be useful. Perhaps there is something special with setOldClass() > which I am no aware of. > > Also, Bioconductor has 100s of packages with S4 where codetools works well. > > Kasper > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >