一起定义具有多个 API 版本的类 [英] Defining classes with several API versions together
本文介绍了一起定义具有多个 API 版本的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这显然不可能...
role Versioned {
method version () {
return self.^api;
}
}
class WithApi:ver<0.0.1>:auth<github:JJ>:api<0> does Versioned {}
class WithApi:ver<0.0.1>:auth<github:JJ>:api<1> does Versioned {}
say WithApi:api<0>.new.version;
say WithApi:api<1>.new.version;
这与
==SORRY!=== Error while compiling /home/jmerelo/progs/perl6/my-perl6-examples/api-versioned.p6
Redeclaration of symbol 'WithApi'
at /home/jmerelo/progs/perl6/my-perl6-examples/api-versioned.p6:11
------> 1>:auth<github:JJ>:api<1> does Versioned⏏ {}
那么是否有可能在一个程序中使用
具有不同api
s、同名的类?
So is it even possible to use
classes with different api
s, same name in a single program?
更新:如果它们包含在不同的文件中,这是获得的错误:
Update: if they are included in different files, this is the error obtained:
P6M Merging GLOBAL symbols failed: duplicate definition of symbol WrongType
推荐答案
在这个例子中,有两件事造成了问题:
Two things are creating a problem in this example:
class
默认是our
,这会导致名称冲突- 类的短名称在外部命名空间中相同,导致冲突
class
is by defaultour
, which causes a name clash- the short name of the class is the same in the outer namespace, causing a clash
如果我们稍微修改一下代码:
If we adapt the code slightly:
role Versioned {
method version () {
return self.^api;
}
}
my constant one = my class WithApi:ver<0.0.1>:auth<github:JJ>:api<1> does Versioned {}
my constant two = my class WithApi:ver<0.0.1>:auth<github:JJ>:api<2> does Versioned {}
say one.version; # 1
say two.version; # 2
我确实发现 :api<0>
有一个错误.显然,这被认为等同于 no :api
设置,导致空字符串而不是 0
.
I did find that there is a bug for :api<0>
. Apparently this is considered to be equivalent to no :api
setting, resulting in an empty string rather than 0
.
这篇关于一起定义具有多个 API 版本的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文