gpt4 book ai didi

grammar - Perl 6是否应该能够从不同的来源中解脱出包含相同角色的问题?

转载 作者:行者123 更新时间:2023-12-03 16:24:10 24 4
gpt4 key购买 nike

想象一下一系列表示为角色的复杂语法,尽管这个简单的例子足以说明冲突:

role Alpha {
token alpha { :i <[A..Z]> }
}

role Digit {
token digit { <[0..9]> }
}

role Either
does Alpha
does Digit {
token either { <alpha> | <digit> }
}

grammar Thingy
does Either
does Alpha
{
token TOP { <alpha> <either>* }
}

my $match = Thingy.parse( '1a3' );
dd $match;

这是行不通的,因为Perl 6不会解开关系以指出冲突实际上是同一来源的同一件事:

Method 'alpha' must be resolved by class Thingy because it exists in multiple roles



但是,阅读 S14,我看到:

A role may not inherit from a class, but may be composed of other roles. However, this "crony" composition is not evaluated until class composition time. This means that if two roles bring in the same crony, there's no conflict--it's just as if the class pulled in the crony role itself and the respective roles didn't. A role may never conflict with itself regardless of its method of incorporation.



我读到这意味着要尽可能晚地应用角色,因此 Thingy类将能够区分 Alpha包含在两个不同的部分中。我认为这可以像创建一个组成 final类的所有角色的列表,然后将该列表仅应用于 final类那样工作。这样,类似 Either的东西将仅混入它定义的内容,并依赖于以后的组合来引入 Alpha

当我尝试为各种(IETF)RFC实现语法时,我遇到了这个问题。他们中的许多人引用了其他RFC的语法,这使得Perl 6无法解决C3的继承问题。因此,我认为角色将断开关系。显然不是。

最佳答案

是的,Perl 6应该能够解开来自不同来源的相同角色。

definition的简单role是:

A role encapsulates some piece of behavior or state that can be shared between classes.



或者

A role is a name for a discrete collection of behaviors.



因此,假设我们对可以漂浮在水上的对象具有 Floatable行为,对于可以航行的对象具有 Sailable行为。自然,可以航行的物体可以漂浮。 Sloop自然是可 float 的和可航行的。在 FloatableFloatable角色都传达 相同的 Sailable行为这一事实上没有冲突。

在Perl中,这可以按预期工作(它也可以与 Moose一起使用):
#!/usr/bin/env perl

use v5.24; # why not
use warnings;

package Floatable {
use Moo::Role;
sub float { say "float" }
}

package Sailable {
use Moo::Role;
with 'Floatable';
sub sail { $_[0]->float; say "sail" };
}

package Sloop {
use Moo;
with qw( Floatable Sailable );
}

my $s = Sloop->new;

$s->sail;

这种行为在直观上是显而易见的。

在查看 Perl6 documentation for roles时,我注意到的一个问题是缺少一个简单的一句话定义:

Roles are in some ways similar to classes, in that they are a collection of attributes and methods. They differ in that roles are also meant for describing only parts of an object's behavior and in how roles are applied to classes. Or to phrase it differently, classes are meant for managing objects and roles are meant for managing behavior and code reuse.



...

Role application differs significantly from class inheritance. When a role is applied to a class, the methods of that role are copied into the class. If multiple roles are applied to the same class, conflicts (e.g. attributes or non-multi methods of the same name) cause a compile-time error, which can be solved by providing a method of the same name in the class.



显然,当 perl6遇到两个提供完全相同行为的角色时,它以我认为不合理的方式将其视为冲突。

请注意以下示例中的细微差别:
#!/usr/bin/env perl

use v5.24; # why not
use warnings;

package Floatable {
use Moo::Role;
sub float { say "float" }
}

package Sailable {
use Moo::Role;
sub float { say "floating bonds to finance journey "}
sub sail { $_[0]->float; say "sail" };
}

package Sloop {
use Moo;
with qw( Floatable Sailable );
}

my $s = Sloop->new;

$s->sail;

在这种情况下,冲突是可以预料的,因为两个不同的角色希望声称提供相同的行为。

关于grammar - Perl 6是否应该能够从不同的来源中解脱出包含相同角色的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41791602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com