孙英凯
2025-Sep-11  01:09 UTC
[Rd] [BUG?] S4 validity function not enforced during object creation in R 4.4.2
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 at 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