in reply to Re^7: Data Structures
in thread Data Structures
That lead me to think of something that might be worthwhile. It'd be cool to have compile-time optimization of simple accessors such that you could redeclare the accessors in the class definition and the simple accessors are no longer optimized and no users of those accessors need to be updated.
So you declare class Foo objects have public members of .x and $foo.x is compiled into very efficient accessing of that member. Down the road, you decide to write an explicit and more complicated accessor for .x. When code using this class gets compiled with "use Foo;" loading the new version of the class, then $y= $foo.x; gets compiled into $y= $foo.x(); while $foo.x= $y; gets compiled into $foo.x( $y );.
Of course, this probably won't happen in Perl 6 because the general case requires \$foo.x to be compiled into something that ties a scalar value such that a fetch of that value calls $foo.x() while a store to that value calls $foo.x( ... ), and history shows that this inefficient general case will likely be used for the simple cases as well. But perhaps Perl 6's design goal of being more easily optimized might change that.
Or we could just restrict such to read-only attributes. Then you could have code doing $y= $foo.x; that doesn't need to be updated (and is as efficient as it can be) and if you have any code that does $foo.x= $y;, then (once you've declared the member variable as requiring an accessor) you'd get a compile-time error telling you exactly what code needs to be changed. You'd need only re-compile ("perl -c") all code in your repository to verify that you don't have anything left to fix.
There could even be a distinction between 1) ".x is private" meaning using $foo.x outside of a method calls the accessor while $self.x inside of a method does a direct access; vs. 2) ".x() is the accessor for ._x", which would require any place in your classes that modifies .x directly to be upgraded to either $foo.x( ... ); or $foo._x= ...;.
Anyway, I don't recall reading of such an idea in Perl 6 design documents, but that was a long time ago so I could easily have forgotten or it could have been introduced since then.
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^9: Data Structures (optimized accessors and Perl 6)
by chromatic (Archbishop) on May 08, 2008 at 23:59 UTC |