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

inheritance + check functions #193

Closed
hadley opened this issue Feb 26, 2022 · 6 comments · Fixed by #213
Closed

inheritance + check functions #193

hadley opened this issue Feb 26, 2022 · 6 comments · Fixed by #213

Comments

@hadley
Copy link
Member

hadley commented Feb 26, 2022

Need equivalent of is()/inherits(), along with a helper that asserts that an object is of a class (extracting code out of the property checkers). Implementation already exists in class_inherits(); just needs a better name.

@hadley
Copy link
Member Author

hadley commented Mar 3, 2022

Other ideas

  • instance (Dylan)
  • isa (Julia)

@hadley hadley changed the title inheritance + check_inhertiance functions inheritance + check functions Mar 4, 2022
@hadley
Copy link
Member Author

hadley commented Mar 7, 2022

Ensure that vector of NA can be coerced to any other vector type.

@lawremi
Copy link
Collaborator

lawremi commented Mar 10, 2022

Would is/inherits() not work already? I thought we were just using the S3 class vector approach.

@lawremi
Copy link
Collaborator

lawremi commented Mar 10, 2022

What do you mean by a vector of NA? Isn't the vector type a separate concept from missingness?

@hadley
Copy link
Member Author

hadley commented Mar 10, 2022

You could use inherits() currently, but you'd have to get the vector of class names somehow. I think you want some function where you can do:

foo <- new_class("foo")
x <- foo()

is_a(x, foo)
#> [1] TRUE

is_a(1, foo)
#> [1] FALSE

The NA case, I think, is needed for property checking of base types, since currently we have:

library(R7)
foo <- new_class("foo", properties = list(x = character))
foo(x = NA)
#> Error: <foo> object properties are invalid:
#> - @x must be <character>, not <logical>

Created on 2022-03-10 by the reprex package (v2.0.1)

which I think is a bit unappealing. Maybe that needs to be handled elsewhere, but property type checking currently uses the internal class_inherits(), which is what I would imagine would power is_a().

@lawremi
Copy link
Collaborator

lawremi commented Mar 14, 2022

Ok, so is_a() would accept the class object instead of the class name, got it.

I'd prefer to handle the NA case as a convenience during construction, while ensuring that properties are always of the correct type, even if every value is NA.

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

Successfully merging a pull request may close this issue.

2 participants