Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:33:00+00:00 2026-06-06T11:33:00+00:00

Lets say I want to make a class myClass with two slots A and

  • 0

Lets say I want to make a class “myClass” with two slots A and B.

now I want a validObject function that ensures A and B are the same length

same_length <- function(object){
    if(length(object@A)!=length(object@B)) {
        "vectors are not the same length"
     } else TRUE
}

setClass("myClass", representation(A="numeric", B="numeric"),
          validity=same_length)

I saw a function somewhere that will ensure the class is valid when initialized:

setMethod("initialize", "myClass", function(.Object, ...){
    value <- callNextMethod()
    validObject(value)
    value
})

which will send an error if I try

newObj <- new(“myClass”, A=c(1,2,3), B=c(1,2))

But if I do

newObj <- new("myClass")
newObj@A <- c(1,2,3)
newObj@B <- c(1,2)

no error is thrown. How do I get it to throw an error as soon as a new slot assignment does not validate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:33:02+00:00Added an answer on June 6, 2026 at 11:33 am

    Write a ‘replacement method’ that does the check. To do this, we need to create a generic function (because no function with the appropriate name and signature already exists)

    setGeneric("slotA<-", function(x, ..., value) standardGeneric("slotA<-"))
    

    We then need to implement the replacement method for the specific types of objects we want to handle — the first argument is of class ‘myClass’, the second argument (value) is of class ‘numeric’:

    setReplaceMethod("slotA", c("myClass", "numeric"), function(x, ..., value) {
        x@A = value
        validObject(x)
        x
    })
    

    We might also write a ‘getter’ generic and method

    setGeneric("slotA", function(x, ...) standardGeneric("slotA"))
    setMethod("slotA", "myClass", function(x, ...) x@A)
    

    and then

    > a=new("myClass", A=1:10, B=10:1)
    > slotA(a)
     [1]  1  2  3  4  5  6  7  8  9 10
    > slotA(a) = 1:5
    Error in validObject(x) : 
      invalid class "myClass" object: vectors are not the same length
    

    Note that the default initialize method calls checkValidity, so if you use callNextMethod as the last line in your constructor there’s no need to explicitly check validity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a PHP class, lets say Myclass.php. Now inside that class
Lets say I have an ActionScript class: MyClass and that class has data in
So lets say I want to make a simple Price class in JS. It's
Lets say I want to make few classes to determine behaviour of agents. The
To make it easy, lets say I have an arraylist allBooks containing class books
Lets say I inherit a class, that has several public properties and/or methods, however
Lets say I have two similar class: public class First { public static final
I want to create a class lets say called enemy 1, enemy 2, and
Lets say I have this program below. Now I want to pass both the
So lets say I want to make a deep copy of an object, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.