问题 Perl 6可以假设一个位置参数不是第一个吗?


Perl 6允许你用咖喱子程序 。假设。当您想要假定领先的位置参数时,这很容易做到:

# The first one
{
sub first-and-last ( $first, $last ) {
    say "Name is $first $last";
    }

my &joe = &first-and-last.assuming( 'Joe' );

&joe.( 'Jones' );
}

但是,如果我想假设其他位置参数之一而只留下第一个位置参数呢?我该怎么说呢 .assuming 假设哪些参数?

# The second one
try {
sub first-and-last ( $first, $last ) {
    say "Name is $first $last";
    }

my &smith = &first-and-last.assuming( Empty, 'Smith' );

&smith.( 'Joe' );
}

使用命名参数这很简单,但这不是我很好奇的。

如果这是真的 只是一个EVAL 在下面,这有点令人失望。


12872
2018-04-27 23:45


起源

FWIW,有 一些 经过广泛的健全检查后,目前正在进行评估。但是如果你确保在编译时发生这种情况,例如通过前缀BEGIN,如: my &smith = BEGIN &first-and-last.assuming( Empty, 'Smith' ); 你不会在运行时遭受任何去优化效果。 - Elizabeth Mattijsen


答案:


嗯,一个 随你 作品:

sub first-and-last ( $first, $last ) {
    say "Name is $first $last";
    }

my &smith = &first-and-last.assuming( *, 'Smith' );

&smith.( 'Joe' );

你可以处理中间参数:

sub longer-names ( $first, $middle, $last, $suffix ) {
    say "Name is $first $middle $last $suffix";
    }

my &smith = &longer-names.assuming( *, *, 'Public', * );

&smith.( 'Joe', 'Q.', 'Jr.');

12
2018-04-28 00:03



很好的例子。我非常喜欢他们,所以我适应了这个 Perl 6文档。这没关系,还是我自己做? - Christopher Bottoms
我都可以。这都是StackOverflow的许可:) - brian d foy