如何在 Perl 6 中返回上下文相关的返回值?

发布于 2024-09-27 20:17:27 字数 344 浏览 3 评论 0 原文

在 Perl 5 和 Perl 6 之间的差异摘要中,值得注意的是 WantArray 函数消失了:

wantarray() 消失了

想要的数组消失了。在 Perl 6 中,上下文 向外流动,这意味着 例程不知道它是哪个上下文 已在。

相反,您应该返回以下对象 在任何情况下都做正确的事。

有人可以提供一个如何创建此类对象的示例吗?

In the summary of differences between Perl 5 and Perl 6, it is noted that the wantarray function is gone:

wantarray() is gone

wantarray is gone. In Perl 6, context
flows outwards, which means that a
routine does not know which context it
is in.

Instead you should return objects that
do the right thing in every context.

Could someone provide an example of how such an object is created?

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

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

发布评论

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

评论(2

泪痕残 2024-10-04 20:17:27

我认为两个例子可能是:


http://perlcabal.org/syn/S13.html#Type_Casting< /a>

类可以定义允许它像例程、数组或散列一样进行响应的方法。长格式如下:

method postcircumfix:<( )> ($capture) {...}
method postcircumfix:<[ ]> (**@slice) {...}
method postcircumfix:<{ }> (**@slice) {...}

这些有点笨拙,所以您也可以使用这些简短的形式:

method &.( $capture ) {...}
method @.[ **@slice ] {...}
method %.{ **@slice } {...}

另外,我认为这可能相关,但不太相关: http://perlcabal.org/syn/S12.html

搜索:

您可以编写自己的访问器来覆盖任何或所有自动生成的访问器。

因此,您返回一个具有多个特定于上下文的访问器的对象。


有趣的是,它开始于 Perl6 用通用的“want”替换“wantarray”: RFC 98 (v1) 基于上下文的方法重载,大约 2000 年,也在 http://dev.perl.org/perl6/rfc/21.html 。我不确定为什么/何时进行更改。

I think 2 examples might be:


http://perlcabal.org/syn/S13.html#Type_Casting

A class may define methods that allow it to respond as if it were a routine, array, or hash. The long forms are as follows:

method postcircumfix:<( )> ($capture) {...}
method postcircumfix:<[ ]> (**@slice) {...}
method postcircumfix:<{ }> (**@slice) {...}

Those are a bit unwieldy, so you may also use these short forms:

method &.( $capture ) {...}
method @.[ **@slice ] {...}
method %.{ **@slice } {...}

Also, I think this might be relevant though less so: http://perlcabal.org/syn/S12.html

Search for:

You may write your own accessors to override any or all of the autogenerated ones.

So you return an object which has several context-specific accessors.


Interestingly enough, it started out with Perl6 replacing "wantarray" with a generic "want": RFC 98 (v1) context-based method overloading, circa 2000, also at http://dev.perl.org/perl6/rfc/21.html . I'm not sure why/when the change was made.

羁绊已千年 2024-10-04 20:17:27

Reddit 上关于这篇博文评论 http://perlgeek.de/blog-en/perl-6/immutable-sigils-and-context.html" rel="nofollow noreferrer">不可变印记和上下文给出了以下示例:

class GeoLocation is Array {
    method Str { 'middle of nowhere' }
}

sub remote_location {
    return GeoLocation.new(1e6 xx 3);
}

# or even easier:

sub remote_location {
    return (1e6 xx 3) but 'middle of nowhere';
}

This comment on Reddit about the blog post Immutable Sigils and Context gives the following examples:

class GeoLocation is Array {
    method Str { 'middle of nowhere' }
}

sub remote_location {
    return GeoLocation.new(1e6 xx 3);
}

# or even easier:

sub remote_location {
    return (1e6 xx 3) but 'middle of nowhere';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文