Skip to content
Prev 63172 / 63421 Next

[BUG?] S4 validity function not enforced during object creation in R 4.4.2

Dear Yingkai,

The default method for 'initialize', which is called by 'new' in your example
and is accessible as

     > selectMethod("initialize", "ANY")

has always called 'validObject' *only if* it finds arguments matching '...',
so the behaviour that you report is not a change from earlier behaviour.

The bug seems to be that you, the class author, defined a nonvirtual class
with an invalid prototype, *not* that 'new' ignores invalid prototypes.

I added the following regression test in Matrix version 1.6-0 in order to
catch exactly this mistake:

     for (cl in c(names(getClassDef("Matrix")@subclasses),
                  names(getClassDef("MatrixFactorization")@subclasses)))
         if (!isVirtualClass(def <- getClassDef(cl)))
             validObject(new(def))

One could argue that setClass(name, ...) should test validObject(new(name))
before returning whenever it defines a nonvirtual class.  But I can imagine
false positives, e.g., if a class author documents that 'new' *must* be
called with additional arguments, then the validity of the prototype seems
inconsequential.  Hence the test ought to be optional and probably (at least
initially) disabled by default.

Mikael
setClass('TestVital',
Session info:
R version 4.4.2 (2024-10-31)
Expected behavior: