Skip to content

Revocable

The interface for an object that can stop being valid, such as a deleted database row.

To revoke an object means to tell the program that the object is no longer valid. A class that implements Revocable gets two methods. revoke() revokes the object, and a second call does nothing. isRevoked() returns true after revoke() was called.

After revoke(), every Core\ObjectMap, Core\ObjectSet and Core\Heap removes the object. A revoked object still exists. A property read or write, a method call, a conversion to a string, a comparison and a foreach on it throw RevokedObjectError. isRevoked(), == and passing or storing the object do not throw.

Good to know: only a class with no parent class can write implements Revocable. Its subclasses are revocable too. A class that declares its own revoke() or isRevoked() does not compile.

The example below shows both methods, and the error a read of a revoked object throws.

Examples

A coupon that can be revoked

<?nvs
// Shows the two methods every `Revocable` class gets: `revoke()` and `isRevoked()`.
final class Coupon implements Revocable
{
public function constructor(public string $code)
{}
}
var $coupon = new Coupon('WINTER');
// Before `revoke()`, `isRevoked()` returns `false`. This prints `valid`.
echo $coupon->isRevoked() ? 'revoked' : 'valid', "\n";
// The second `revoke()` does nothing. This prints `revoked`.
$coupon->revoke();
$coupon->revoke();
echo $coupon->isRevoked() ? 'revoked' : 'valid', "\n";
// Reading `code` now throws `RevokedObjectError`. The message names line 16, the first `revoke()`.
try {
echo $coupon->code, "\n";
} catch (RevokedObjectError $e) {
echo $e->message, "\n";
}
Output
valid
revoked
This `Coupon` object was revoked in `docs/examples/types/Revocable/01-a-coupon-that-can-be-revoked.nvs` on line 16. A revoked object cannot be used.

Changelog

VersionChange
0.0.5A 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.