Search code examples
rakumop

Defining classes with several API versions together


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

Solution

  • 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.