为什么 Perl 6 的 unwrap 方法是 Routine 的方法? [英] Why is Perl 6's unwrap method a method of Routine?

查看:31
本文介绍了为什么 Perl 6 的 unwrap 方法是 Routine 的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个 unwrap 方法,但它似乎是我应该使用的方式这不是它应该使用的方式.似乎它应该是一个独立例程或不同类中的方法.我错过了什么?

There's an unwrap method, but the way it seems I'm supposed to use it isn't the way it should be used. It seems like it should either be a standalone routine or a method in a different class. What am I missing?

它似乎并不关心它的调用者是什么,只要它获取正确的 Routine::WrapHandle 东西作为参数.在这例如,我wrap一个子程序并返回一个WrapHandle:

It appears that it doesn't care what its invocant is as long as it gets the right Routine::WrapHandle thingy as an argument. In this example, I wrap a subroutine and get back a WrapHandle:

sub add-two-numbers ( $n, $m ) { $n + $m }

sub do-some-stuff ( $n, $m, $o ) {
    add-two-numbers(  $n max $m, $m max $o );
    }

put do-some-stuff( 5, 10, 15 );

# now I want to look into do-some-stuff to see what it's
# passing
my $wraphandle = &add-two-numbers.wrap: {
    say "Arguments were (@_[])";
    callwith( |@_ );
    }

put do-some-stuff( 5, 10, 15 );

然后,我可以创建一个不同且不相关的例程并调用 unwrap对此:

Then, I can create a different and unrelated routine and call unwrap on that:

my &routine = sub { 1 };
&routine.unwrap( $wraphandle );

put do-some-stuff( 5, 10, 15 );

unwrap 的调用者似乎是多余的.确实,我可以称之为类方法,它仍然有效:

The invocant to unwrap seems superfluous. Indeed, I can call it as a class method and it still works:

Routine.unwrap( $wraphandle );

但是,这似乎应该是一个例行程序(因为调用者没关系):

But, it seems this should either be a routine (since the invocant doesn't matter):

unwrap( $wraphandle ); # doesn't exist

或者 Routine::WrapHandle 上的一个方法,因为那是行为:

Or a method on Routine::WrapHandle since that's the source of the behavior:

$wraphandle.unwrap;  # but, this method doesn't exist in that class

那么,我错过了这个方法什么?

So, what am I missing about this method?

推荐答案

据猜测,界面的设计方式是一种(例程保留信息并能够删除被动"句柄),但实现了另一种方式(使用句柄已经保存了所有必需的信息,因此它可以自行解包).

At a guess, the interface was designed one way (with the routine keeping the information and being able to remove 'passive' handles), but implemented another (with the handle already keeping all required information so it can unwrap itself).

至于 unwrap 应该是句柄上的一个方法的概念:它实际上是,但该方法称为 restore,其中 Routine.unwrap 仅委托给 (cf /Routine.pm:110..113):

As to the notion that unwrap should perhaps be a method on the handle: It actually is, but the method is called restore, which Routine.unwrap merely delegates to (cf core/Routine.pm:110..113):

method unwrap($handle) {
    $handle.can('restore') && $handle.restore() ||
        X::Routine::Unwrap.new.throw
}

如果你想要完整的故事,除了 core/Routine.pm,还有 Perl6::Metamodel::WrapDispatcher 定义在 Perl6/Metamodel/Dispatchers.nqp.就我所见,我推测的原始设计当然应该可以实现,但需要有人对这个问题有足够强烈的感觉才能真正做到这一点......

If you want the full story, besides core/Routine.pm, there's also Perl6::Metamodel::WrapDispatcher defined in Perl6/Metamodel/Dispatchers.nqp. From what I can see, it certainly should be possible to implement the original design I conjectured, but it would need someone feeling strongly enough about the issue to actually do it...

这篇关于为什么 Perl 6 的 unwrap 方法是 Routine 的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆