Initializing a persistable class field with a constant leads to linker error

Here’s a persistable class and its weak_map:

using { /Verse.org/Simulation }

var MyClassMap:weak_map(player, my_class) = map{}

my_class := class<final><persistable>:
    MyField:int = 0

^ This code compiles with no problem.

I want to use a constant to initialize my field, so I add it:

using { /Verse.org/Simulation }

MyDefaultValue:int = 0

var MyClassMap:weak_map(player, my_class) = map{}

my_class := class<final><persistable>:
    MyField:int = MyDefaultValue

Now this won’t compile anymore, I get this error:

Script error 9000: Linker task graph contains a cycle:
    (1,1, 1,1): Verse compiler info: Linking object '_Root'
    myclass.verse(8,14, 9,34): Verse compiler info: Linking object 'my_class'
    (1,1, 1,1): Verse compiler info: Linking object '_Root' 

The error goes away if I remove the weak_map declaration.

Am I doing something wrong or is this a Verse bug?

Hey @Romanito how are you?

I’ve been testing this and what I think is hapenning is that ‘_Root’ (which is the top-level scope where “MyDefaultValue” lives) has a dependency on “my_class”, as you are using it in “MyClassMap”. But, at the same time, “my_class” has a dependency on ‘_Root’ because you are trying to use “MyDefaultValue”.

That “cycle” could be causing the error you are seeing. And it stops giving you errores when you remove the map because ‘_Root’ doesn’t have a dependency from “my_class” anymore, breaking the cycle.

The best thing you can do is to initialize the variable “MyField” as you do in the first code example you shared.

Hope this makes sense!

Hi @BRGJuanCruzMK,

That makes sense, thanks for your explanation. This code is a simplified example but this default value is used in multiple places in my project, hence the use of a global constant. For the time being I’m not using this constant for the field initialization but I’m using it at the other places.

1 Like