RevokedObjectError
RevokedObjectError is the error a program gets when it uses a revoked object. An object is revoked when its class implements Revocable and the program called revoke() on it. The error is a LogicError, which is the error class for a mistake in the program itself.
Every use of a revoked object throws it. This includes a property read or write, a method call, a conversion to a string, a comparison, a foreach, clone, Core\Json::encode and Core\Reflect. isRevoked(), ==, is, ::class and passing or storing the object do not throw it. Its message names the class and the file and line of the first revoke(): "This Blog\Post object was revoked in src/Blog/Post.nvs on line 42. A revoked object cannot be used."
Call isRevoked() before you use an object that may be revoked. isRevoked() never throws.
The example below shows a deleted post that is still in a plain array, and the error that a read of its title throws.
Examples
Catching a use after delete
<?nvs// Shows the error you get when a program reads a post that it already deleted.
final class Post implements Revocable{ public function constructor(public string $title) {}
public function delete(): void { $this->revoke(); }}
var $hello = new Post('Hello');var $news = new Post('News');array<Post> $posts = [$hello, $news];
// `delete` revokes the post. The plain array `$posts` still contains it.$news->delete();
// The loop prints `Hello`. Then reading the title of the deleted post throws `RevokedObjectError`.// The message names the class `Post` and line 11, where `revoke()` was called.try { foreach ($posts as Post $post) { echo $post->title, "\n"; }} catch (RevokedObjectError $e) { echo $e->message, "\n";}
// `isRevoked()` never throws. This loop skips the deleted post and prints only `Hello`.foreach ($posts as Post $post) { if ($post->isRevoked()) { continue; } echo $post->title, "\n";}HelloThis `Post` object was revoked in `docs/examples/types/RevokedObjectError/01-catching-a-use-after-delete.nvs` on line 11. A revoked object cannot be used.HelloChangelog
| Version | Change |
|---|---|
0.0.5 | A class with no parent class can implement Revocable, so that the program can say an object is no longer valid. After $object->revoke(), every Core\ObjectMap, Core\ObjectSet and Core\Heap removes the object immediately. Any other use of it throws RevokedObjectError, and its message names the line of the revoke() call. |
0.0.6 | Every use of a revoked object (an object after a call to its revoke() method) throws RevokedObjectError. This includes .= on a property, an inout property argument, a call of a method reference and isinit. It also includes a conversion to a string, a comparison, a foreach, a delegated method and a static call through the object. |
