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

孙英凯 @uny|ngk@| @end|ng |rom @jtu@edu@cn
Thu Sep 11 03:09:25 CEST 2025


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 using 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



More information about the R-devel mailing list