Journal(2005) | Blog(2006) | RandomLink | WhoAmI | LiveBookmark | HomePage

<<Previous: modperl 的 PerlTransHandler 应用  >>Next: 用 prototype 来写 Ctrl+Enter 提交表单

C3 method resolution order

Category: Perl6   Keywords: C3 method resolution order

介绍 C3 方案之前,我们先看看 Perl 5 的多重继承方案。
package A;
sub hello { 'A::hello' }

package B;
use base 'A';

package C;
use base 'A';
sub hello { 'C::hello' }

package D;
use base ('B', 'C');

这是一个经典的多重继承,用图表表示如下:
    <A>(hello)
/ \
<B> <C>(hello)
\ /
<D>
在 Perl 5 中当你调用 D::hello 时会比较奇怪地发现它输出的是 A::hello.
这是因为在 Perl 5 中继承的顺序是 D->B->A->C
而按照我们一般的思路顺序则应当是 D->B->C->A 输出的应当为 C::hello

在 Perl6 中我们就采用了 C3 这种算法,这种算法首先是在 Dylan 语言中实现。而后被应用到 CLOS 还有 Python 2.3。
现在我们将它应用到了 Perl6 和 parrot. Perl6 的 Perl6-MetaModel 作者 Stevan Little 用 Perl5 写了个版本,就是 Class::C3

这种算法的思路就是父类一定在子类后面。
比如前面的多重继承可以这么写:D, B, A, C, A
在 Perl 5 中去掉后后面重复的,而 C3 算法是把前面的父类去掉。

详细的 C3 算法介绍等我看完那篇 Paper 后,如果有不吐不快之言,再写 journal.

<<Previous: modperl 的 PerlTransHandler 应用  >>Next: 用 prototype 来写 Ctrl+Enter 提交表单

Options: +Del.icio.us

Related items Created on 2005-11-25 17:36:06, Last modified on 2005-11-25 17:41:35
Copyright 2004-2005 All Rights Reserved. Powered by Eplanet && Catalyst 5.62.