From @|mon@urb@nek @end|ng |rom R-project@org  Wed Jun 14 02:01:04 2023
From: @|mon@urb@nek @end|ng |rom R-project@org (Simon Urbanek)
Date: Wed, 14 Jun 2023 12:01:04 +1200
Subject: [Rd] codetools wrongly complains about lazy evaluation in S4
 methods
In-Reply-To: <CAC2h7uu_8m=xC7KcB1i_uk=WW8bDvx4P7ACSPOp-s1PK=1nGQg@mail.gmail.com>
References: <2cde2aab-8491-2f99-657e-ecb41ceda039@gmail.com>
 <CAC2h7uu_8m=xC7KcB1i_uk=WW8bDvx4P7ACSPOp-s1PK=1nGQg@mail.gmail.com>
Message-ID: <ED9261FE-2E8C-4312-B388-0FF18652E973@R-project.org>

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
> 



[Rd] codetools wrongly complains about lazy evaluation in S4 methods
Simon Urbanek