我可以在Perl 6中使用独立签名作为签名吗?

时间:2017-01-08 09:49:42

标签: signature perl6 raku

我正在玩一个带有多个开关的命令行程序的Perl 6实现。 MAIN的签名非常复杂,有点混乱。我想知道是否有办法在其他地方定义签名并告诉子程序使用什么:

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

multi MAIN ( $sig ) {
    put "Got $n and $m";
    }
即使在MAIN编译之前设置了签名,

MAIN也看不到签名中的变量:

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

我认为在编译时生成方法并根据各种因素选择签名可能很方便。

1 个答案:

答案 0 :(得分:9)

简答:否

答案很长:仍然没有。如果可以的话,你会遇到各种各样的麻烦,因为你的例子中只有一个变量sub f(Int $n, Int $m) { # do something sensible here with $n and $m } constant &MAIN = &f; # reuse &f for more 。如果您多次使用签名(或仅使用一次,并递归到您附加到它的子例程),则每个签名绑定都会覆盖该变量中的先前值,从而导致一些距离的怪异动作。根本没有一种机制来克隆自由浮动签名中涉及的变量。

在实际例程中使用match-all签名,然后使用它来绑定捕获到签名遭遇同样的缺陷的解决方法,因此恕我直言不值得麻烦。

也许在遥远的未来,一些非常先进的宏功能可能允许您移植签名,但我认为宏观开发的一部分现在甚至不在我们的路线图上。

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

django_1            | 2017-01-09 08:24:44,328 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
django_1            | 2017-01-09 08:24:44,329 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
django_1            | 2017-01-09 08:24:44,331 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
django_1            | 2017-01-09 08:24:44,331 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
django_1            | Unhandled exception in thread started by <function wrapper at 0x7ff06bee5d70>
django_1            | Traceback (most recent call last):
django_1            |   File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
django_1            |     fn(*args, **kwargs)
django_1            |   File "/usr/local/lib/python2.7/site-packages/channels/management/commands/runserver.py", line 84, in inner_run
django_1            | Performing system checks...
django_1            | 
django_1            | System check identified no issues (0 silenced).
django_1            | January 09, 2017 - 08:24:44
django_1            | Django version 1.10.4, using settings 'backend.settings'
django_1            | Starting Channels development server at http://0.0.0.0:8000/
django_1            | Channel layer default (asgi_redis.core.RedisChannelLayer)
django_1            | Quit the server with CONTROL-C.
django_1            |     ws_protocols=getattr(settings, 'CHANNELS_WS_PROTOCOLS', None),
django_1            |   File "/usr/local/lib/python2.7/site-packages/daphne/server.py", line 41, in __init__
django_1            |     ''' % self.__class__.__name__)
django_1            | DeprecationWarning: 
django_1            |                 The host/port/unix_socket/file_descriptor keyword arguments to Server are deprecated.
django_1            |