Reassigning a WeakRef's value?

,

After creating w = WeakRef([1]), is there any problem with reassigning w.value = [2], both before or after its value is cleared to nothing by GC.gc()?

I can not find anything on this, nor can I observe any unexpected behaviour (so far).

Do you know more?

The question has been unanswered for 19 hours. So I believe most of the people have no idea about this API—including myself.

To me, I don’t think this API carries much value, for a Julia programmer aiming to solve high-level practical problems.

amd@amd:~/julia_projects/uc/Uc2$ ./j
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.12.7 (2026-08-15)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org release
|__/                   |

julia> w = WeakRef([1]);

julia> w
WeakRef([1])

julia> w.value
1-element Vector{Int64}:
 1

julia> w
WeakRef([1])

julia> 
amd@amd:~/julia_projects/uc/Uc2$ ./j
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.12.7 (2026-08-15)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org release
|__/                   |

julia> w = WeakRef([1]);

julia> w
WeakRef([1])

julia> w
WeakRef([1])

julia> w.value

julia> w
WeakRef(nothing)

This example shows that its behavior is nondeterministic.

I completely disagree. WeakRef’s behaviour is nondeterministic because when GC happens is nondeterministic. You example shows nothing other than GC sometimes happens and sometimes does not. That is expected behaviour.

@skraemer : I had Claude look through the implementation, and it claims it does work with mutation. It would be nice to document, though. Please make an issue or a PR to Julia’s GitHub repo.

Mutation works fine with WeakRef.

My impression was that the constructor of WeakRef just once creates a pointer that is tracked differently, but does ultimately not care much about what the pointer points at. If that holds up, mutation should indeed work fine.

Concerning the GC, I think it may rather work epistemically unpredictably, but in any case, I agree that it this is expected, and it is not particular to WeakRef.

@jakobnissen Should you want to, please feel free to open the issue yourself. Should I eventually decide to do so, I will make sure to first have a look if it was already filed.

Hilariously, certain AI has already picked up this thread and is now, quite opposed to its earlier statements, rather easily convinced that of course it is okay.