# How unique are \`gensym\` symbols?

**URL:** <https://discourse.julialang.org/t/how-unique-are-gensym-symbols/134509>\
**Category:** General Usage\
**Created:** [December 12, 2025, 12:11am UTC](https://discourse.julialang.org/t/how-unique-are-gensym-symbols/134509 "2025-12-12T00:11:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [December 12, 2025, 12:11am UTC](https://discourse.julialang.org/t/how-unique-are-gensym-symbols/134509/1 "2025-12-12T00:11:01Z")

</div>

The documentation for `gensym` says that this function

> generates a symbol which will not conflict with other variable names (in the same module).

When playing around with it, I get the impression that the symbols are actually globally unique:

```julia-auto
julia> gensym()
Symbol("##277")

julia> @eval Base gensym()
Symbol("##278")

julia> module A end; @eval A gensym()
Symbol("##279")

```

Can I assume that `gensym` returns globally unique symbols?

The motivation for my question is that I want to manually sanitize an expression returned by a macro (and that contains itself a macro call). I can explain that in more detail if anybody is interested. The key point is that I want to get a symbol that doesn’t conflict with any identifier in the module where my macro is called. However, `gensym` would only guarantee that for the module containing the definition of my macro. I’ve figured out already that something like

```julia-auto
newsym = @eval __module__ gensym()

```

inside the macro definition wouldn’t work because for nested macro calls, ` __module__ ` is not the module of the immediate caller, but that of the top level call site.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 12, 2025, 12:20am UTC](https://discourse.julialang.org/t/how-unique-are-gensym-symbols/134509/2 "2025-12-12T00:20:15Z")

</div>

The note about a module is incorrect and will be fixed in v1.14. What’s really going on is that these names are globally unique within a julia session. However, precompilation of packages provides a mechanism by which names from different sessions can be imported into new sessions.

This means that e.g. if you made a package

```julia-auto
module MyPackage

x = gensym()

end

```

and then you precompiled `MyPackage`, you could end up in a situation where

```julia-auto
julia> using MyPackage

julia> MyPkackage.x == gensym()
true

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 12, 2025, 12:54am UTC](https://discourse.julialang.org/t/how-unique-are-gensym-symbols/134509/3 "2025-12-12T00:54:49Z")

</div>

See also [Precompiled gensyms are not unique · Issue #44903 · JuliaLang/julia](https://github.com/JuliaLang/julia/issues/44903) for more details. It’s a bit of a footgun.
