Explain the UniquedById example in the manual

The manual section Module initialization and precompilation gives this example of a bad pattern:

mutable struct UniquedById
    myid::Int
    let counter = 0
        UniquedById() = new(counter += 1)
    end
end

All subsequent usages of this incrementally compiled module will start from that same counter value.

I am trying to recreate the full scenario where this code becomes an issue. I can put this code in a package and use it in a script, the package will be precompiled and cached. What would be my next steps that will reveal the issue?

1 Like

After you put this code in a package UniquedByIds, you can create another package A. A can use UniquedByIds to e.g. create a global object of UniquedById.

When you now use both packages from the REPL and create another object of UniquedById in Main and compare this object with the global object in A, you’ll see the problem.

Thank you, that works.