为什么字段pragma与Perl中的多重继承不兼容? [英] Why is the fields pragma incompatible with multiple inheritance in Perl?

查看:63
本文介绍了为什么字段pragma与Perl中的多重继承不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多重继承很棒,只要您对继承层次结构和一些潜在的陷阱有清楚的了解(例如字段编译指示的禁止.确实,我找不到关于此的任何文档...

Multiple inheritance is great, and Perl handles it just fine as long as you have a clear understanding of your inheritance heirarchy and some of the potential pitfalls (such as those described in perldoc perltoot). Yet it does not discuss the prohibition of using the fields pragma with multiple inheritance. Indeed, I can find no documentation about this whatsoever...

这是一个例子:

package Parent1;
use fields 'field1';

package Parent2;
use fields 'field2';

package Child;
use base qw(Parent1 Parent2);

此操作失败,并显示以下错误:无法在...处继承继承字段"

This fails with the error: "Can't multiply inherit fields at..."

即使父母双方都具有相同的字段,这也不起作用.甚至,当他们证明是相同的时候,因为他们来自共同的祖父母:

This doesn't work even when both parents have the same fields.. even when they are provably the same, because they come from a shared grandparent:

package Grandparent;
use fields qw(field1);

package Parent1;
use base 'Grandparent';

package Parent2;
use base 'Grandparent';

package Child;
use base qw(Parent1 Parent2);

正确实现此目标的一个陷阱是不变性,即子对象中字段的索引始终与其父对象中的索引相同.我不确定是否确实需要此要求,但是...与C ++不同,在C ++中,可以使用键入父对象的指针来访问对象,Perl在对其引用进行操作时始终知道对象的真实类型(实际上是字段pseudohash本质上是一个vtable,保存在每个对象实例上).特别是在上面的第二个示例中,从每个父级继承的字段都来自都是父级,因此可以将它们折叠在一起,并且不会出现冲突的索引.

One pitfall to implementing this properly is the invariant that the index of a field in a child object is always the same as the index in its parent. I'm not sure this requirement is truly needed, however... unlike in C++, where an object may be accessed using a pointer typed to a parent, Perl always knows the real type of an object when operating on its reference (indeed the fields pseudohash is essentially a vtable, kept on every single object instance). And particularly in the second example above, the fields inherited from each parent come from both parents, so they can be folded together and there is no conflicting index.

我敢肯定还有其他问题,但我还没有发现.

I'm sure there are other issues as well, but I haven't yet found them.

任何了解Perl内部知识的人都可以对此发表评论吗?

Can anyone with some knowledge of Perl's internals comment on this?

推荐答案

1)您可以尝试使用委托而不是继承

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

2)另外,一些文档(包括上面的链接)似乎暗示多重继承的问题是由于伪哈希引起的. Perl 5.10将字段"编译指示的实现更改为伪哈希以外的其他方法-如果可以的话,请尝试在Perl5.10中使用的方法,它可能会起作用(我没有访问5.10的权限,因此无法进行实验,抱歉)

2) Also, some documentation (including the link above) seems to imply that the problem with the multiple inheritance is because of pseudohashes. Perl 5.10 changed the implementation of "fields" pragma to something other than pseudohashes - if it's an option, try the approach you used in Perl5.10 and it just might work (I don't have acces to 5.10 so can't experiment, sorry)

P.S.关于我找不到任何关于此的文档……"-至少在官方"文档中对此进行了提及是引用自骆驼书(O'Reilly的Perl系列,"Programming Perl")第三版,第三章31.3. 使用基础":

P.S. Regarding "I can find no documentation about this whatsoever..." - at least one mention of this in "official" documentation is a quote from the Camel book ("Programming Perl", O'Reilly's Perl series), 3rd Edition, chapter 31.3. "use base":

不支持字段类的多重继承.如果一个以上的命名基类具有字段,则使用基本编译指示会引发异常."

"Multiple inheritance of field classes is not supported. The use base pragma raises an exception if more than one named base class has fields."

这篇关于为什么字段pragma与Perl中的多重继承不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆