Why are pragma fields incompatible with multiple inheritance in Perl?

Multiple inheritance is great, and Perl handles it just fine if you have a clear understanding of your hereditary hierarchy and some potential errors (such as those described in perldoc perltoot ). However, he does not discuss the prohibition of using fields pragmas with multiple inheritance. In fact, I cannot find documentation about this at all ...

Here is an illustration:

package Parent1; use fields 'field1'; package Parent2; use fields 'field2'; package Child; use base qw(Parent1 Parent2); 

Error with the error: "Unable to multiply the inheritance of fields in ..."

This does not work, even if both parents have the same fields .. even if they are provably the same, because they come from grandfather and grandmother:

 package Grandparent; use fields qw(field1); package Parent1; use base 'Grandparent'; package Parent2; use base 'Grandparent'; package Child; use base qw(Parent1 Parent2); 

One lie for the correct implementation of this method is the invariant that the index of the field in the child always coincides with the index in its parent. I'm not sure if this requirement is really necessary, however ... unlike C ++, where an object can be accessed using a pointer typed for a parent, Perl always knows the actual type of an object when working by its reference (indeed, fields pseudohash is essentially a vtable stored on every instance of an object). And especially in the second example above, the fields inherited from each parent come from both parents, so they can be added together and there is no conflicting index.

I am sure there are other problems, but I have not found them yet.

Can anyone with some knowledge of the internal Perl components comment on this?

+3
source share
2 answers

1) You can try to use delegation instead of inheritance as described here .

2) In addition, some documents (including the link above) seem to imply that the problem of multiple inheritance is related to pseudo-checks. Perl 5.10 changed the implementation of the "fields" pragma to something other than pseudo-shahs - if this is an option, try the approach you used in Perl5.10 and it just can work (I do not have access to 5.10, so you can’t experiment, sorry)

PS Concerning “I can’t find any documentation about this at all ...” - at least one mention of this in the “official” documentation is a quote from the book of Camel ("Programming Perl", O'Reilly Perl series), 3- e edition, chapter 31.3. "use base":

"Multiple inheritance of field classes is not supported. The database usage pragma throws an exception if more than one base class name has fields."

+4
source

The pragma field was an interesting experiment, but, in my opinion, unsuccessful. This is the most useful feature; checking the compilation time of attribute hash keys was removed in 5.10. There are a number of decent modules for class composition on CPAN, as well as in heavy weight, but Moose's popularity is growing steadily ("Postmodern Object System for Perl 5").

However, if you want to make an attempt to add support for multiple inheritance, this is likely to be welcome; note that fields are now supported regardless of perl as part of the "base" distribution . However, you would have to use it for both pseudo-check based (pre-5.9.0) and limited hash versions (5.9.0+).

+3
source

Source: https://habr.com/ru/post/1312263/


All Articles