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
Date: Thu, 11 Sep 2025 09:09:25 +0800 (CST)
From: =?gb2312?B?y+/Tor+t?=<[email protected]>
Dear R Core Team,
I would like to report a possible bug or behavioral change in the S4 class
system in R 4.4.2 regarding the validity function.
Description of the issue:
In previous versions of R, when the validity function of an S4 class returns a
character string (i.e., an error message), the new() function would refuse to
create the object and throw an error.
However, in R 4.4.2, I found that new() creates the object successfully even
when the validity function returns an error message. Only a manual call to
validObject() triggers the error.
Minimal reproducible example:
setClass('TestVital',
slots = list(visit_type='character'),
prototype = list(visit_type=''),
validity = function(object){
if(!object@visit_type %in% c('OP','IP')){
return('来访类型错误')
}
TRUE
})
new('TestVital') # This should fail, but it succeeds in R 4.4.2
validObject(new('TestVital')) # This correctly triggers the error
Session info:
R version 4.4.2 (2024-10-31)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.6.1
Expected behavior:
new('TestVital') should fail and throw an error if the validity function
returns a character string, as documented in the official R extensions manual.
Actual behavior:
new('TestVital') creates the object even when the validity function returns an
error message.
Is this an intentional change in R 4.4.x, or is it a bug?
Thank you for your attention.
Best regards,
Sun Yingkai
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel