On Dec 10, 2004, at 11:05 AM, Abhijit Mahabal wrote:
Consider a class (e.g., the hypothetical Geometry::Triangle) that can have several attributes (side1, side2, side3, angle1, ang_bisector1, side_bisector, altitude1 and so forth), most of which will not be needed for most instances of Geometry::Triangle.
I know how this can be done in P5. Using the layout "Hash" things are easy. How can P6 deal with this, without allocating too much memory to things that'd never get used? The layout "P6Hash" could come to the rescue, but there is still the issue of syntax:
what exactly does C<has $.bisector1> do? IIRC, in the P6Opaque layout, every instance of the class would have space the size of a PMC allocated for it. This behavior is not needed for P6Hash, and it should just leave attributes alone until they are assigned to (where defaults are also "assigns").
In which case, maybe for that layout we can get away without declaring all attributes, perhaps? (Since the declaration does nothing except help the parser).
I was thinking whether we could do something like this:
class Triangle is layout<P6Hash> does autovivify{ method calculate_bisectors { $.bisector1 = ...; # $.bisector1 autovivifies } }
where it is an error without the autovivify, and only P6Hash supports autovivification.
If it's an error without the autovivify and only P6Hash supports autovivify, then isn't this redundant? Simply saying 'P6Hash' should get me the autovivify behavior for free. (Incidentally that is 'should' as in 'that is the way I think it would make sense for it to work in an ideal universe' not 'should' as in 'based on the implementation, here is the behavior I expect').
If, for some reason, I DON'T want that behavior, then I can pick another layout or explicitly say 'does noautovivify'.
Incidentally, I just want to go on record as saying that the verbosity of class declarations in P6 is really starting to skeeve me. I keep reminding myself that these are the edge cases that are being discussed, that you don't need all this stuff for the common case (right?) that Perl 6 can be pretty much the same easy, succint, fun language that I know and love, that sensible defaults will be chosen so that in the common case the things I get automatically are the things I wanted anyway. But, occasionally, I still find myself going "eeew...Java."