【问题标题】:Defining classes with several API versions together一起定义具有多个 API 版本的类
【发布时间】:2020-04-05 07:39:03
【问题描述】:

这显然是不可能的......

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

那么甚至可以在一个程序中使用不同的apis、同名的use 类吗?

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

P6M Merging GLOBAL symbols failed: duplicate definition of symbol WrongType

【问题讨论】:

    标签: raku mop


    【解决方案1】:

    在这个例子中有两件事造成了问题:

    • class 默认为 our,这会导致名称冲突
    • 类的短名称在外部命名空间中相同,导致冲突

    如果我们稍微修改一下代码:

    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&lt;0&gt; 存在错误。显然这被认为等同于 no :api 设置,导致空字符串而不是 0

    【讨论】:

    • 这将如何转化为创建具有不同 api 版本的类的两个文件?导出的符号需要使用不同的名称吗?
    • 在我们实现对:as 的支持之前,这样做的一种方法是将文件导入两个不同的块中。比如:constant Foo = do { use Test; Test }; dd Foo; # Test
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多