简体   繁体   English

Any.match有什么作用?

[英]What does Any.match do?

It has the deceivingly simple code: 它有一个欺骗性的简单代码:

 method match(Any:U: |) { self.Str; nqp::getlexcaller('$/') = Nil }

However, this is the behavior it has: 但是,这是它的行为:

(^3).match(1) # OUTPUT: «「1」␤»

So far, so good. 到现在为止还挺好。

say (1,3 ... * ).match(77); # OUTPUT: «Nil␤»

Ooookey. Ooookey。 What's happenning now? 现在发生了什么?

say (1,3 ... * ).match(1);    # OUTPUT: «Nil␤»
say (1,3 ... * ).match(/\d/); # OUTPUT: «Nil␤»

Does not like sequences. 不喜欢序列。

say (^10).match(/\d/); # OUTPUT: «「0」␤»

OK, makes sense again. 好的,再次有意义。

say <a b c>.match(/\w/); # OUTPUT: «「a」␤»

Back to normal. 恢复正常。 So is it that it does not like Seqs? 它不喜欢Seqs吗? I assume, because I've looked at the other classes' code and match is not reimplemented, all of them are calling that code. 我假设,因为我查看了其他类的代码并且match没有重新实现,所有这些都调用了该代码。 But I fail to see how returning a string and setting a variable from NPQ does that, or why it does not work on sequences. 但是我没有看到如何返回字符串并从NPQ设置变量,或者为什么它不能用于序列。

.match is a search for a needle in a single haystack string. .match是在单个haystack字符串中搜索一个针。 An infinite sequence stringifies to '...' . 无限序列字符串为'...'

say (1,3 ... 9).Str;        # 1 3 5 7 9
say (1,3 ... 9).match: '1'; # 「1」

say (1,3 ... *).Str;        # ...
say (1,3 ... *).match: '.'; # 「.」

How I worked this out 我是如何解决这个问题的

First, you're looking at the wrong method definition: 首先,您正在查看错误的方法定义:

method match(Any:U: |) { ... }

Any:U is kinda like Any $ where not .defined except if it matched you would get the error message "Parameter '<anon>' of routine 'match' must be a type object of type 'Any', not an object instance ...". Any:U有点像Any $ where not .defined除非它匹配你会得到错误消息“参数'<anon>'例程'匹配'必须是'任何'类型的类型对象,而不是对象实例。 ..“。

But you're passing a defined Seq . 但是你传递了一个定义的 Seq So your .match calls don't dispatch to the method definition you're looking at. 因此,您的.match调用不会调度到您正在查看的方法定义。

To find out what a method dispatches to, use: 要找出方法调度的内容,请使用:

say (1,3 ... *).^lookup('match').package ; # (Cool)

A defined Seq will thus dispatch to the Cool code : 因此, 定义的 Seq将分派给Cool代码

method match(Cool:D: |c) {
    ...
    self.Stringy.match(|c)
}

So, next: 那么,下一个:

say (1,3 ... *).^lookup('Stringy').package ; # (Mu)

And the code : 代码

multi method Stringy(Mu:D $:) { self.Str }

So check: 所以检查:

say (1,3 ... *).Str; # ...

Bingo. 答对了。

And confirm: 并确认:

say (1,3 ... *).match: '.'; # 「.」

The accepted answer explains perfectly what's going on. 接受的答案完全解释了发生了什么。 I'm just adding this to show some examples of how to do what the OP seems to have intended. 我只是添加这个来展示如何做OP似乎有意的事情的一些例子。

doug$ perl6
To exit type 'exit' or '^D'
> say (^3).any.match(/\d/)
any(「0」, 「1」, 「2」)
> say (^3).any.match(/\d/).so
True
> say (^3).any.match(/ <alpha> /).so
False
> say ('a'..'c').any.match(/ <alpha> /).so
True
> # say (0 ... *).any.match(/ \d /).so ## ==> never terminates
Nil
> say (0 ... *).first(/ \d /)
0
> say (0 ... *).first(/ \d\d /)
10
>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM