用几个 API 版本一起定义 类

Defining classes with several API versions together

这显然不可能...

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⏏ {}

那么,是否有可能 use 类 在同一个程序中使用不同的 api 和相同的名称?

更新:如果它们包含在不同的文件中,这是得到的错误:

P6M Merging GLOBAL symbols failed: duplicate definition of symbol WrongType

在此示例中,有两件事造成了问题:

  • class 默认为 our,这会导致名称冲突
  • 外层命名空间class的短名称相同,造成冲突

如果我们稍微调整一下代码:

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.