Re: Design Question: Subclass-specific Constants
Hi Shlomi, 2. A more philosophical reason (and more major) is that I feel that "has()" > fields, which are stored inside the object's instance are *instance-based > data* while what I'm looking for is global class/subclass-based data and > constants. > I've used MooseX::ClassAttribute for errr.. class attributes: class_has '_parameter_name' => ( ... ); which seems to addess the philosophical differences between instance and class attributes (i.e. optimisation aside, IMO it improves code readability). Cheers, -- Ian Sillitoe
Re: Design Question: Subclass-specific Constants
On Tuesday 27 Apr 2010 19:17:25 Dave Rolsky wrote: > On Tue, 27 Apr 2010, Shlomi Fish wrote: > > Wouldn't it mean that perl will call the _build_paragraph_name of the > > sub- class upon every instantiation of an object? I could be prematurely > > micro- optimising in thinking that it matters, though. > > Yes, you are prematurely optimizing. > True, however, there are different reasons why I dislike having such a class/type-based constant as a has field: 1. One can mutate it in the instance (including at run-time) by assigning to $self->{_bold_tag_name}. 2. A more philosophical reason (and more major) is that I feel that "has()" fields, which are stored inside the object's instance are *instance-based data* while what I'm looking for is global class/subclass-based data and constants. As a result, I think I'll go with my original way, but maybe use "required ''" to make sure such a method is defined in the non-abstract and production-ready subclasses. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Rethinking CPAN - http://shlom.in/rethinking-cpan God considered inflicting XSLT as the tenth plague of Egypt, but then decided against it because he thought it would be too evil. Please reply to list if it's a mailing list post - http://shlom.in/reply .
Re: Design Question: Subclass-specific Constants
On Tue, 27 Apr 2010, Shlomi Fish wrote: Wouldn't it mean that perl will call the _build_paragraph_name of the sub- class upon every instantiation of an object? I could be prematurely micro- optimising in thinking that it matters, though. Yes, you are prematurely optimizing. As an aside, naming an attribute with a name starting with "get" is an abomination. If you want PBP style accessors, use MooseX::FollowPBP. Attribute names should be nouns, not verbs. Well, first of all, I used _get_ here in my code as a clarification because I used it in the sub-class-mutated-constant-method previously. I realise it's not wise to use _get_something() for attributes/properties/fields/object- members/etc. (like has '_get_something'). Otherwise you can have code like '$self->_get_something("New Value");'. That put aside, I sometimes prefix methods returning calculations (not object properties) with "get_" or "calc_". Whether *that* is a good idea, is much more of a colour of the bike shed argument, and it probably doesn't matter too much. Right, _methods_ should be verbs, and attributes (declared with "has") should be nouns. -dave /* http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) */
Re: Design Question: Subclass-specific Constants
On Tuesday 27 Apr 2010 18:54:06 Dave Rolsky wrote: > On Tue, 27 Apr 2010, Shlomi Fish wrote: > > [code] > > has '_get_paragraph_tag_name' => (is => 'ro', default => "para"); > > [/code] > > > > And maybe specialise it further using «has '+_get_paragraph_tag_name'», > > but it seems like it would be much more verbose. > > I would do this ... > > has _paragraph_name => ( is => 'ro', builder => '_build_paragraph_name' > ); > > Then in the parent class: > > sub _build_paragraph_name { > die 'This method must be overridden in the child' > } > Wouldn't it mean that perl will call the _build_paragraph_name of the sub- class upon every instantiation of an object? I could be prematurely micro- optimising in thinking that it matters, though. > As an aside, naming an attribute with a name starting with "get" is an > abomination. If you want PBP style accessors, use MooseX::FollowPBP. > Attribute names should be nouns, not verbs. Well, first of all, I used _get_ here in my code as a clarification because I used it in the sub-class-mutated-constant-method previously. I realise it's not wise to use _get_something() for attributes/properties/fields/object- members/etc. (like has '_get_something'). Otherwise you can have code like '$self->_get_something("New Value");'. That put aside, I sometimes prefix methods returning calculations (not object properties) with "get_" or "calc_". Whether *that* is a good idea, is much more of a colour of the bike shed argument, and it probably doesn't matter too much. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ "The Human Hacking Field Guide" - http://shlom.in/hhfg God considered inflicting XSLT as the tenth plague of Egypt, but then decided against it because he thought it would be too evil. Please reply to list if it's a mailing list post - http://shlom.in/reply .
Re: Design Question: Subclass-specific Constants
On Tue, 27 Apr 2010, Hans Dieter Pearcey wrote: On Tue, 27 Apr 2010 10:54:06 -0500 (CDT), Dave Rolsky wrote: has _paragraph_name => ( is => 'ro', builder => '_build_paragraph_name' ); Then in the parent class: sub _build_paragraph_name { die 'This method must be overridden in the child' } Alternately, turn your parent class into a role that contains _gen_paragraph_tag and requires _build_paragraph_name. This has the advantage of failing immediately when composed into a class without the required method, instead of dying at runtime when it's called. I agree, that's much better. -dave /* http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) */
Re: Design Question: Subclass-specific Constants
- Original Message > From: Dave Rolsky > I would do this ... > has _paragraph_name => ( is => 'ro', builder => '_build_paragraph_name' ); > Then in the parent class: > sub _build_paragraph_name { > die 'This method must be overridden in the child' >} Why not just define a "paragraph" role which requires this? Have it be an interface for composition-time safety (or add extra methods which might need to be shared). package Role::Paragraph; use Moose::Role; requires '_parapgraph_name'; # ... 1; Cheers, Ovid -- Buy the book - http://www.oreilly.com/catalog/perlhks/ Tech blog - http://blogs.perl.org/users/ovid/ Twitter - http://twitter.com/OvidPerl Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
Re: Design Question: Subclass-specific Constants
On Tue, 27 Apr 2010 10:54:06 -0500 (CDT), Dave Rolsky wrote: > has _paragraph_name => ( is => 'ro', builder => '_build_paragraph_name' ); > > Then in the parent class: > > sub _build_paragraph_name { > die 'This method must be overridden in the child' > } Alternately, turn your parent class into a role that contains _gen_paragraph_tag and requires _build_paragraph_name. This has the advantage of failing immediately when composed into a class without the required method, instead of dying at runtime when it's called. hdp.
Re: Design Question: Subclass-specific Constants
On Tue, 27 Apr 2010, Shlomi Fish wrote: [code] has '_get_paragraph_tag_name' => (is => 'ro', default => "para"); [/code] And maybe specialise it further using «has '+_get_paragraph_tag_name'», but it seems like it would be much more verbose. I would do this ... has _paragraph_name => ( is => 'ro', builder => '_build_paragraph_name' ); Then in the parent class: sub _build_paragraph_name { die 'This method must be overridden in the child' } As an aside, naming an attribute with a name starting with "get" is an abomination. If you want PBP style accessors, use MooseX::FollowPBP. Attribute names should be nouns, not verbs. -dave /* http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) */