Search code examples
portingraku

"Illegally post-declared types" with circular class definition


Rakudo version 2020.01

This does not compile.

use v6;

class N1 {}
class T1 {}

class G1 {
    has @.g is required where A1|Q1;
}

class Q1 {
    has $.q is required where N1|T1|G1;
}

class A1 {
    has Q1 @.a is required;
}

===SORRY!=== Error while compiling …
Illegally post-declared types:
    A1 used at line 7
    Q1 used at line 7

I thought that declaring just the identifiers ahead of G1 would help me, but this also fails:

===SORRY!=== Error while compiling …
Too late for unit-scoped class definition;
Please use the block form.
…:6
------> class A1;⏏ class Q1;

How do I make it work?


Solution

  • You can, but you need to define the classes as a stub.

    class A { }
    

    will just define an empty class. Whereas:

    class A { ... }   # note the yadayadayada
    

    will define a stub. So adding:

    class A1 { ... }
    class Q1 { ... }
    

    to the top of your code, should fix the problem.