|
I would like to check if an object is in invariant form after all of the final initialization is performed in the :Init attributed subroutine. If the object is in an "improper" form, I would like to fail construction, just as field type validators throw an OIO::Args error. I realize that I can do this manually, but such functionality should probably be built in. For example, maybe some :Restricted function in Object::InsideOut itself that one could call from an :Init subroutine that would perform:
OIO::Args->die(...)
... with some supplied parameters.
Here is a somewhat contrived example that demonstrates the need and you can decide on the best approach:
sub _init :Init
{
# A car should not have any sort of wings as one of its parts
# Note that this test involves two members of the class and could potentially
# test base class members or functions on them, as well.
if ($vehicle_type[$$self] eq 'car' && grep {/\Wwings\W/} @{$parts[$$self]})
{
$self->fail_init(
'message' => 'A car should not have wings',
'Info' => 'A wings part was added to a vehicle of type car',
); # fail_init should do whatever it needs to and result in a die()
# We never get to this point, because of the early exit due to fail_init
}
}
Thanks,
Michael Goldshteyn |