is Code
WhateverCode
对象是 Whatever
-priming 的结果。有关详细信息,请参阅 Whatever
文档。
当您希望控制方法或函数解释任何Whatever star的方式时,您可以使用多重分派和Whatever
和WhateverCode
参数来执行此操作,如下例所示
multi get-val(Cycle , Int )# Define what to do with a stand-alone * as the second argumentmulti get-val(Cycle , Whatever )# Define what to do with a * WhateverCode in an expressionmulti get-val(Cycle , WhateverCode )my Cycle .= new(:pos(2), :vals(0..^10));say get-val(, 3); # OUTPUT: «3»say get-val(, *); # OUTPUT: «2»say get-val(, *-1); # OUTPUT: «1»
WhateverCode
does
Callable
角色,因此应该可以内省它包含的Callable
类型;例如,继续前面的示例,我们可以通过检查签名添加一个处理带有两个参数的WhateverCode
的多重
# Define what to do with two * in an expressionmulti get-val(Cycle , WhateverCode where )say get-val(, * + * div 2); # 2 + 10/2 = 7
但是,请注意,子表达式可能会施加它们自己的Whatever star规则
my = (0, 1, 2);say get-val(, []) # 2, because the star belongs to the Array class
这会让Whatever stars的所有权变得混乱,所以小心不要做得太过。
您还可以使用Callable
类型进行类型约束,以便接受任何Callable
,包括WhateverCode
sub run-with-rand (Callable ) ;run-with-rand *.say; # OUTPUT: «0.773672071688484»run-with-rand ; # OUTPUT: «0.38673179353983»run-with-rand sub ; # OUTPUT: «0.0589543603685792»
使用&
标记的参数进行类型约束效果同样好,并且输入更短
sub run-with-rand () ;