gensym

gensym([tag])

Generates a symbol which will not conflict with other variable names.

Examples

gensym([tag])

Generate a symbol which will not conflict with other variable names.

julia> gensym()
Symbol("##439")

julia> gensym("temp")
Symbol("##temp#440")

Here are some common examples of using gensym:

  1. Create a unique symbol without a tag:

    julia> gensym()
    Symbol("##441")

    This example generates a unique symbol without providing a specific tag.

  2. Create a unique symbol with a tag:

    julia> gensym("temp")
    Symbol("##temp#442")

    In this example, a unique symbol is generated with the tag "temp".

  3. Avoid variable naming conflicts:
    julia> x = 5;
    julia> y = gensym("x");
    julia> eval(:($y = 10))
    10
    julia> x
    5
    julia> x == eval(y)
    false

    By using gensym, you can generate a symbol that does not conflict with existing variable names. In this case, the symbol generated with the tag "x" (gensym("x")) is assigned a different value than the existing variable x.

Common mistake example:

julia> gensym(x)
ERROR: UndefVarError: x not defined
Stacktrace:
...

In this example, the variable x is used as an argument to gensym without being defined. It's important to provide a string as the argument to gensym, which serves as a tag, rather than a variable name directly.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: