Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusion in 15.3.4 #1715

Open
sebffischer opened this issue Dec 13, 2021 · 1 comment
Open

Confusion in 15.3.4 #1715

sebffischer opened this issue Dec 13, 2021 · 1 comment

Comments

@sebffischer
Copy link

The section mentions, that the helper to construct a Person object converts the age to a double so that it also works with integers, however the following is fine for me

setClass("Person",
  slots = c(
    name = "character",
    age = "numeric"
  )
)

john = new("Person", name = "John Smith", age = 12L)

Created on 2021-12-13 by the reprex package (v2.0.1)

What exactly is meant here?

@sebffischer
Copy link
Author

Ok, admittedly

setClass("Person",
  slots = c(
    name = "character",
    age = "numeric"
  )
)
Person <- function(name, age = NA) {
  new("Person", name = name, age = age)
}

Person("John Smith")
#> Error in validObject(.Object): invalid class "Person" object: invalid object for slot "age" in class "Person": got class "logical", should be or extend class "numeric"

Created on 2021-12-13 by the reprex package (v2.0.1)

but then it would be clearer if one would just use

Person <- function(name, age = NA_real_) {
  new("Person", name = name, age = age)
}

Created on 2021-12-13 by the reprex package (v2.0.1)

otherwise it kind of suggests that intergers are not numeric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant