-
Notifications
You must be signed in to change notification settings - Fork 38
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
Parent validators are called multiple times during object construction #248
Comments
I’m pretty sure child classes already have to be valid instances of the parent. Do you have some reason to suspect otherwise or do we need to document this better? |
Ah, you're right, it does run the ancestor validator(s) on their descendents.
From the documentation (which I know is WIP), my impression was that validation
It was not clear to me that it also runs the parent validator function. It would be helpful to
|
While poking around at this, I did notice that it seems to run the ancestor validators multiple times:
Is this expected behavior? |
Oh good catch, that's definitely not desired behavior! |
Oh this is because the way that the grandchild constructor works is to create a parent object, then use that to construct a child object, then use that to construct a grandchild object. So maybe we should hold off on constructor validation until the final object is created? i.e. maybe we just need a |
Note to self: need to fix #249 before this to make debugging easier. |
Updated/somewhat more minimal reprex: library(S7)
parent <- new_class("parent", validator = function(self) cat("parent\n"))
child <- new_class("child", parent, validator = function(self) cat("child\n"))
grandchild <- new_class("grandchild", child, validator = function(self) cat("grandchild\n"))
parent()
#> parent
#> <parent>
child()
#> parent
#> child
#> parent
#> <child>
grandchild()
#> parent
#> child
#> parent
#> grandchild
#> child
#> parent
#> <grandchild> Created on 2023-04-14 with reprex v2.0.2 |
Naively this feels like it should be true, particularly if the parent class comes next in line when looking up methods. I.e. if I have a parent class
pet
, and child classesdog
andcat
, a validdog
object should also be a validpet
object.On the other hand I imagine it could get slow/complicated to be running through a whole chain of ancestor validators. And possibly a child class might not want to conform to the parent validator if it's too strict, and belongs to somebody else so can't be adjusted. So is this the sort of thing that should be handled by trying to behave politely instead by enforcing it?
If that's the case, If I'm creating a child class, is there a way I could choose to run the parent class's validator on my objects if I'm trying to play nicely?
I don't have much of an OOP background, so I don't know how this is handled in S4 or in other languages.
The text was updated successfully, but these errors were encountered: