加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl6 – 我可以选择没有参数的Perl 6 multis吗?

发布时间:2020-12-15 21:49:00 所属栏目:大数据 来源:网络整理
导读:我可以根据一些非参数值选择一个多,但我必须至少有一个参数,所以我可以克服那里的位置: our $*DEBUG = 1;debug( 'This should print','Phrase 2' );$*DEBUG = 0;debug( 'This should not print' );multi debug ( *@a where ? $*DEBUG ) { put @a }multi deb
我可以根据一些非参数值选择一个多,但我必须至少有一个参数,所以我可以克服那里的位置:
our $*DEBUG = 1;
debug( 'This should print','Phrase 2' );

$*DEBUG = 0;
debug( 'This should not print' );

multi debug ( *@a where ? $*DEBUG ) { put @a }
multi debug ( *@a where ! $*DEBUG ) { True }

我似乎记得有人曾经在multis中发送一些没有参数的技巧.例如,我有一个show-env例程,我想要四处展开,如果我设置了一些调试条件,它只会做任何事情.我可以像我所展示的那样实现它,但这并不是很令人满意,而且我想象的其他地方并不聪明:

our $*DEBUG = 1;
debug( 'This should print','Phrase 2' );
show-env();

$*DEBUG = 0;
debug( 'This should not print' );
show-env();

multi debug ( *@a where ? $*DEBUG ) { put @a }
multi debug ( *@a where ! $*DEBUG ) { True }

# use an unnamed capture | but insist it has 0 arguments
multi show-env ( | where { $_.elems == 0 and ? $*DEBUG } ) { dd %*ENV }
multi show-env ( | where { $_.elems == 0 and ! $*DEBUG } ) { True }

我可以用可选的命名参数做类似的事情,但这更不令人满意.

当然,我可以在这个简单的例子中做到这一点,但这并不好玩:

sub show-env () {
    return True unless $*DEBUG;
    dd %*ENV;
    }

解决方法

你可以解构|用().
my $*DEBUG = 1;
show-env();

$*DEBUG = 0;
show-env();

# use an unnamed capture | but insist it has 0 arguments by destructuring
multi show-env ( | ()   where ? $*DEBUG ) { dd %*ENV }
multi show-env ( | ()   where ! $*DEBUG ) { True }

show-env(42); # Cannot resolve caller show-env(42); …

或者你可以有一个原型声明

proto show-env (){*}
multi show-env ( |      where ? $*DEBUG ) { dd %*ENV }
multi show-env ( |      where ! $*DEBUG ) { True }

show-env(42); # Calling show-env(Int) will never work with proto signature () …

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读