作为对this question about using different APIs in a single program,Liz Mattijsen suggested to use constants的跟踪。现在这是一个不同的用例:让我们尝试创建一个按API版本进行区分的multi
,如下所示:
multi
我们将常量用于第二个版本,因为两个符号不能在单个符号空间中在一起。但这会产生此错误:
class WithApi:ver<0.0.1>:auth<github:JJ>:api<1> {}
my constant two = my class WithApi:ver<0.0.1>:auth<github:JJ>:api<2> {}
multi sub get-api( WithApi $foo where .^api() == 1 ) {
return "That's version 1";
}
multi sub get-api( WithApi $foo where .^api() == 2 ) {
return "That's version deuce";
}
say get-api(WithApi.new);
say two.new.^api;
say get-api(two.new);
所以That's version 1
2
Cannot resolve caller get-api(WithApi.new); none of these signatures match:
(WithApi $foo where { ... })
(WithApi $foo where { ... })
in block <unit> at ./version-signature.p6 line 18
返回正确的api版本,调用者是say two.new.^api;
,所以get-api(WithApi.new)
具有正确的类型和正确的API版本,但是未调用multi吗?我在这里缺少什么吗?
解决方案非常简单:还要别名“ 1”版本:
伊丽莎白·马蒂森say one.^name; # one
say two.^name; # two
。签名匹配符号,而不是符号名称。但是,当您将别名(使用常量)命名为新名称时,仍会保留该名称。让我们使用它进行统一的多重调用,其中唯一更改的是api版本: