raku-弦调用匿名正则

发布于 2025-02-09 18:32:35 字数 349 浏览 0 评论 0原文

为什么第一个表达式解释但不解释第二个?
我理解它们是相同的,也就是说,一个弦乐的字符串。

("foo").first: /foo/ andthen say "$_ bar";
> foo bar
"foo": /foo/ andthen say "$_ bar";
> Confused
> at /home/dmc7z/raku/foo.raku:2
> ------> "foo":⏏ /foo/ andthen say "$_ bar";
>     expecting any of:
>         colon pair

Why does the first expression interpret but not the second?
I understand them to be identical, that is, a string invoking an anonymous regex.

("foo").first: /foo/ andthen say "$_ bar";
> foo bar
"foo": /foo/ andthen say "$_ bar";
> Confused
> at /home/dmc7z/raku/foo.raku:2
> ------> "foo":⏏ /foo/ andthen say "$_ bar";
>     expecting any of:
>         colon pair

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2025-02-16 18:32:35

这是一个方法调用:

 ("foo").first: /foo/

它与您写过的书相同:

("foo").first( /foo/ )

或仅:(

"foo".first( /foo/ )

请注意,我在上面的三个说明的末尾使用了。这就是使用:意味着以下内容是同一表达式的一部分。)

在这种情况下,使用first并不是很有意义。相反,我将使用~~

"foo" ~~ /foo/ andthen say "$_ bar";

第一个用于查找与某些描述匹配的列表中的第一个项目。如果您在单个项目上使用它,它将返回项目,或返回nil。它总是返回一个值,在这种情况下,nil是最明智的单个未定义值。


它说它期望结肠对的原因是的唯一用途可能在该位置有效。老实说,我希望它抱怨它是一个无效的标签。

This is a method call:

 ("foo").first: /foo/

It's the same as if you had written:

("foo").first( /foo/ )

Or just:

"foo".first( /foo/ )

(Note that I used : at the end of the three above descriptions in English. That's where the idea to use : to mean that whatever following it is part of the same expression comes from.)

In this case it doesn't make a whole lot of sense to use first. Instead I would use ~~.

"foo" ~~ /foo/ andthen say "$_ bar";

first is used to find the first item in a list that matches some description. If you use it on a single item it will either return the item, or return Nil. It always returns one value, and Nil is the most sensible single undefined value in this case.


The reason it says it's expecting a colon pair is that is the only use of : that could be valid in that location. Honestly I halfway expected it to complain that it was an invalid label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文