Defining classes with several API versions together

孤者浪人 提交于 2020-05-29 02:37:09

问题


That's not apparently possible...

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;

This dies with

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

So is it even possible to use classes with different apis, 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

回答1:


Two things are creating a problem in this example:

  • class is by default our, 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

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.



来源:https://stackoverflow.com/questions/61039465/defining-classes-with-several-api-versions-together

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!