"Illegally post-declared types" with circular class definition
Asked Answered
C

1

5

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?

Contaminate answered 3/2, 2020 at 14:55 Comment(0)
E
10

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.

Entertainment answered 3/2, 2020 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.