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

underlying data corrupt #519

Open
sims1253 opened this issue Jan 4, 2025 · 2 comments
Open

underlying data corrupt #519

sims1253 opened this issue Jan 4, 2025 · 2 comments

Comments

@sims1253
Copy link

sims1253 commented Jan 4, 2025

While trying to get something to work, I got to this minimal example, which resulted in an error that sounded like something in the S7 internals broke. Maybe I did someting obviously wrong, but I figured I'd file an issue just in case:

Error: <TestClass> object is invalid:
- Underlying data is corrupt
4.
stop(msg, call. = FALSE)
3.
function (object, recursive = TRUE, properties = TRUE)
{
check_is_S7(object)
if (!is.null(attr(object, ".should_validate"))) { ...
2.
`prop<-`(`*tmp*`, "x", value = 10)
1.
TestClass(x = 10)

Minimal Example:

TestClass <- S7::new_class(
    "TestClass",
    properties = list(
        x = S7::new_property(default = NULL, validator = function(value) {
            if (!is.null(value) && !is.numeric(value)) {
                "@x must be numeric"
            }
        })
    ),
    constructor = function(x = NULL) {
        obj <- new_object(.parent = TestClass)
        if (!is.null(x)) {
            prop(obj, "x") <- x
        }
        obj
    }
)

test_obj <- TestClass(x = 10)
@hadley
Copy link
Member

hadley commented Jan 6, 2025

Hmmmm, the problem is that you're trying to work with the object before it's finished construction. I'm not sure if we could make a better error message here, but it would be nice if possible.

@sims1253
Copy link
Author

sims1253 commented Jan 6, 2025

I suppose that would be a more informative error message :) It sounds a lot less like something internal is broken.

In case you're interested how I ended up here: This started with me trying to use (...) in the constructor which didn't work as do.call can't call new_object and I don't know another way of of calling a function with a list of unknown arguments.
Then I figured I'd just NULL all the properties that haven't been passed, as is.null felt clearer than if(length>0) for all the numeric()'s it created for empty properties instead. Which I didn't get to work the way I wanted and ultimately ended up as this example while trying to figure out what I was doing wrong.

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

2 participants