On Fri, Dec 10, 2004 at 11:05:32AM -0500, 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.
This sounds to me more like a situation where you want to use mixins, if you're thinking of it as different kinds of triangles.
But more likely, you just want a single private hash attribute that is manipulated by as many publicly facing methods as you like. Method declarations don't cost you anything on a per-object basis unless they're autogenerated from an attribute. But a hash attribute autovivifies just like any ordinary hash variable, and is probably clearer as well to whoever has to read your code.
So, apart from declaring the methods, I just need to write
.bisector1 = ...;
instead of
$.bisector1 = ...;
Yes, that looks almost as nice as with the $, though perhaps a tad less. That leaves the issue of writing several methods that look like:
method bisector1() is rw { return %:att<bisector1> };Here is an attempt to do this using traits:
#============================================
role hash_attributes {
has %:att;
multi sub trait_auxillary:<has>(hash_attributes $trait,
Class $container :
[EMAIL PROTECTED]
){
$container does hash_attributes;
for @atts -> $att {
&($container)::($att) := method () is rw { return %:att{$att} };
}
}
}class Geometry::Triangle
has hash_attributes <== <side1 side2 side3 angle1 angle2 angle3 ...>
{ method calculate_sides(){
.side1 = ...;
}
}#=============================================
Does that look about right?
I'd just use a hash attribute. All other things being equal, go for simple and standard.
Does it look about standard? (especially the bit using the pipe operator)...
Thanks, abhijit
