我在玩这个小玩意儿,一个集合可以通过翻转测试来作为它的补充。为了实现这一点,我创建了特殊版本的成员资格操作符。
class Complement {
has $.set;
}
multi infix:<∈> ( $a, Complement:D $c ) { $a ∉ $c.set }
multi infix:<∉> ( $a, Complement:D $c ) { $a ∈ $c.set }
my $set = (1, 2, 3).Set;
my $c = Complement.new: set => $set;
put 3 ∈ $set;
put 4 ∈ $c;根据我对infix:<∉>的定义,另一种更普遍的定义似乎已经消失了。没有其他候选者:
True
Cannot resolve caller infix:<∉>(Int, Set); none of these signatures match:
($a, Complement:D $c)
in sub infix:<∈> at /Users/brian/Desktop/complement.p6 line 11
in block <unit> at /Users/brian/Desktop/complement.p6 line 18我需要做什么才能保留具有不同签名的以前的定义?
发布于 2018-02-16 21:09:57
这是因为&infix:<∈>是multi,而&infix:<∉>不是:
$ perl6 -e '&infix:<∈>.candidates.head.multi.say'
True
$ perl6 -e '&infix:<∉>.candidates.head.multi.say'
False当你定义你的multi &infix:<∉>时,你从核心中隐藏了only &infix:<∉>,这就是为什么你只能看到你的候选者。
我们可能会检测到这种情况,并要求用户提供明确的proto来阐明他们的意思。我以R#1530的身份提交的。我要说的是,一个op是multi而另一个op是only的差异是拥有一致核心的更大问题的一部分;我在RT#130020上添加了它作为评论,并在我们关于D#1783的文档中提到了这个问题。
您可以这样定义您的自定义op (如果实现了R#1530中的建议,则需要proto;目前不需要):
proto infix:<∉> (|) {*}
multi infix:<∉> ($a, Complement:D $c) { $a ∈ $c.set }
multi infix:<∉> (|c) { &CORE::infix:<∉>(|c) }我只能猜测您试图编写的代码,但我想我应该提一下,以便您在创建时考虑到它:被否定的操作被定义为not A (op) B,而不是A (not(op)) B。当涉及到Junctions时,这种微小的差异会产生影响
$ perl6 -e 'say 2 ≠ 2|4'
False它之所以能在REPL中工作,是因为实际上存在一个bug (RT#131900),在这个bug中,自定义操作在REPL行之间丢失,所以就像BEGIN块一样,当调用带有去往核心候选对象的arg时,自定义multi不再在作用域中。
https://stackoverflow.com/questions/48819031
复制相似问题