Re: Possible RFC: UniqueInterface that throws exception at refcount > 1

From: Date: Sat, 11 Jul 2020 14:32:00 +0000
Subject: Re: Possible RFC: UniqueInterface that throws exception at refcount > 1
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-110941@lists.php.net to get a copy of this message
Hi Olle Härstedt, > I'd like to discuss the concept of ownership in PHP, from the point of > view of a new interface UniqueInterface (or SplUniqueInterface or > something), which will throw a UniqueException if refcount > 1. It'd seem like it would cause a lot of places where UniqueException could be thrown unexpectedly. Additionally, inspecting the stack trace of an exception would also increment the refcount, causing yet another exception. ``` <?php class UniqueObject{} function test(UniqueObject $x) {     // ...     // handle some edge case     // $trace includes an array with a reference to $x at $trace[0]['args'][0],     // increasing the refcount to 2, throwing.     //     // Error handlers and issue handlers would also have this issues,     // as well as Throwable->getTrace()     $trace = debug_backtrace();     var_dump($trace); } test(new UniqueObject()); ``` > Implementation should be straight-forward, assuming refcount is manipulated at a > single place in the source. This would also introduce some performance overhead if every function that incremented the reference count of a value now had to check if objects implemented that interface. I'd assume it would be significant. This is something that makes more sense in compiled languages like rust that can enforce uniqueness at compile time, and the dynamic nature of php makes that less practical. - Tyson

« previous php.internals (#110941) next »