raku-弦调用匿名正则
为什么第一个表达式解释但不解释第二个?
我理解它们是相同的,也就是说,一个弦乐的字符串。
("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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个方法调用:
它与您写过的书相同:
或仅:(
请注意,我在上面的三个说明的末尾使用了
:
。这就是使用:意味着以下内容是同一表达式的一部分。)
在这种情况下,使用
first
并不是很有意义。相反,我将使用~~
。第一个
用于查找与某些描述匹配的列表中的第一个项目。如果您在单个项目上使用它,它将返回项目,或返回nil
。它总是返回一个值,在这种情况下,nil
是最明智的单个未定义值。它说它期望结肠对的原因是
:
的唯一用途可能在该位置有效。老实说,我希望它抱怨它是一个无效的标签。This is a method call:
It's the same as if you had written:
Or just:
(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~~
.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 returnNil
. It always returns one value, andNil
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.