我可以在 Perl 6 中使用独立的签名作为签名吗? [英] Can I use a standalone Signature as a signature in Perl 6?

查看:60
本文介绍了我可以在 Perl 6 中使用独立的签名作为签名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个命令行程序的 Perl 6 实现,它需要几个开关.MAIN 的签名相当复杂,而且有点乱.我想知道是否有一种方法可以在其他地方定义签名并告诉子程序使用什么:

I was playing around with a Perl 6 implementation of a command-line program that takes several switches. The signature to MAIN was quite involved and a bit messy. I wondered if there was a way to define the signature somewhere else and tell the subroutine what to use:

# possibly big and messy signature
my $sig;
BEGIN { $sig = :( Int $n, Int $m ) };

multi MAIN ( $sig ) {
    put "Got $n and $m";
    }

MAIN 即使在 MAIN 编译之前设置了签名,也看不到签名中的变量:

MAIN doesn't see the variables in the signature even though the signature is set before MAIN compiles:

===SORRY!=== Error while compiling /Users/brian/Desktop/signature.p6
Variable '$n' is not declared
at /Users/brian/Desktop/signature.p6:7

我认为这种事情对于在编译后生成方法和根据各种因素选择签名可能很方便.

I figured this sort of thing might be handy for generating methods after compile time and selecting signatures based on various factors.

推荐答案

简短回答:否

长答案:仍然没有.如果可以,您会遇到各种麻烦,因为在您的示例中只有一个变量 $n.如果多次使用签名(或只使用一次,然后递归到附加到它的子例程中),每个签名绑定都会覆盖该变量中的前一个值,从而导致一些远距离的诡异动作.根本没有克隆自由浮动签名中涉及的变量的机制.

Long answer: Still no. And if you could, you'd run into all kinds of trouble, because there's only one variable $n in your example. If you use the signature multiple times (or use it only once, and recurse into the subroutine that you attached it to), each signature binding would overwrite the previous value in that variable, causing some spooky action at a distance. There simply isn't a mechanism to cloning the variables involved in a free-floating signature.

像在实际例程中使用匹配所有签名,然后使用它来将捕获绑定到签名等解决方法也存在同样的缺陷,因此恕我直言不值得麻烦.

Workarounds like using a match-all signature in the actual routine, and then use that to bind the capture to the signature suffer from this same flaw, and are thus IMHO not worth the trouble.

也许在不久的将来,一些非常先进的宏功能可能会允许您移植签名,但我认为这部分宏开发现在甚至不在我们的路线图上.

Maybe in the far future, some very advanced Macro feature might allow you to transplant signatures, but I don't think that part of macro development is even on our roadmap right now.

您应该重用(可能是匿名的)带有签名的例程,而不是重复使用裸签名,并使用您想要的名称安装它们,例如

Instead of reusing bare signatures, you should reuse (possibly anonymous) routines with a signature attached, and install them with the name you want, for example

sub f(Int $n, Int $m) {
    # do something sensible here with $n and $m
}
constant &MAIN = &f;
# reuse &f for more 

这篇关于我可以在 Perl 6 中使用独立的签名作为签名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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