# Programatic function (and variable) names

**URL:** <https://discourse.julialang.org/t/programatic-function-and-variable-names/101861>\
**Category:** General Usage\
**Tags:** question, macros, functions\
**Created:** [July 21, 2023, 2:13am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861 "2023-07-21T02:13:25Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anton\_Hinneck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anton_hinneck/32/22716_2.png) [@Anton\_Hinneck](https://discourse.julialang.org/u/Anton_Hinneck)\
**Post date:** [July 21, 2023, 2:13am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/1 "2023-07-21T02:13:25Z")

</div>

I have a use case, where code needs to generate multiple sets of functions.  
So functions do not get overwritten, I want to add a unique pre-/postfix equal for each function in a set.

Specifically, functions

- init()
- run()
- …  
are created for every set.

How do I name all functions in the nth set of functions

- init\_n()
- run\_n()
- …

Suppose, the definition of functions stays the same, I just want to change their names.  
This probably falls into the area of macro programming but I could not find an illustrative example that would have helped me.

I want such functionality:  
a = “Function\_name”  
@macro a()  
println("Hello)  
end

Function\_name()  
Hello

I would be happy for any pointer you may be able to provide.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 21, 2023, 2:25am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/2 "2023-07-21T02:25:43Z")

</div>

> [@Anton\_Hinneck](#):
>
> I have a use case, where code needs to generate multiple sets of functions

Why not use anonymous functions? Can you give an example of what kind of function you want to generate, and why?

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 21, 2023, 3:05am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/3 "2023-07-21T03:05:42Z")

</div>

You could do something like this, but such module surgery is not necessarily recommended (I am pretty confident there is a better way to achieve your goals without doing such things):

```julia
function rename_function!(f, newname::Symbol)
    eval(:(global $(newname) = $f))

    # without renaming here also - 
    # the function name will still be print/shown with the old name
    typeof(eval(newname)).name.mt.name = newname
end

function fgenerator()
    eval(:(init_1() = "initial name: init_1"))
end

fgenerator()
@info names(Main) # init_1 already defined

rename_function!(init_1, :init_2)

@info names(Main) # init_2 defined, init_1 printing as init_2

@info init_2() # prints "initial name: init_1"

# shown as init_2
@info init_2

# also shown as init_2
@info init_1

# revert
rename_function!(init_2, :init_1)

# shown as init_1
@info init_2

# also shown as init_1
@info init_1

```

At this point, I am unaware of any method to delete a name once defined.

Again, while the above code is _possible_, it is not something that I would recommend doing.

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [July 21, 2023, 4:18am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/4 "2023-07-21T04:18:32Z")

</div>

The macro you’re looking for is `@eval`:

```julia
a = Symbol("function_name")
@eval function $a()
    println("Hello")
end

```

```julia
julia> function_name()
Hello

```

This is often used for things like programmatically generating wrappers for external libraries (look at the BLAS and LAPACK wrappers in the LinearAlgebra standard library).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 21, 2023, 6:22am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/5 "2023-07-21T06:22:06Z")

</div>

This does not seem like a use case for `eval`. Dynamically generating identifiers/names is universally discouraged in every programming language. (Google “eval is evil” or something like that).

Use anonymous functions. Your use case is not very clear, but you could for example collect them in a vector, and call them as `init[n]()`.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 21, 2023, 6:26am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/6 "2023-07-21T06:26:58Z")

</div>

> [@Anton\_Hinneck](#):
>
> Suppose, the definition of functions stays the same, I just want to change their names.

Making many functions with the exact same body means you’re going to compile their function calls to identical native code many times, which is overhead to be avoided if possible. What is a set here, and why do they need init, run, etc each?

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [July 21, 2023, 6:34am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/7 "2023-07-21T06:34:21Z")

</div>

To expand on what others have said, a vector of functions could be a much better alternative than creating a set of similar functions with generated names. It could look like that:

```julia
julia> const init = map(1:5) do n
          function () # Anonymous function but will be available as init[n]
             println("Hello, I'm function $n")
           end
       end
5-element Vector{var"#6#8"{Int64}}:
   [...]

julia> init[3]()
Hello, I'm function 3

```

---

<div class="post-metadata">

**Author:** ![Anton\_Hinneck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anton_hinneck/32/22716_2.png) [@Anton\_Hinneck](https://discourse.julialang.org/u/Anton_Hinneck)\
**Post date:** [July 21, 2023, 11:59am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/8 "2023-07-21T11:59:56Z")

</div>

Thanks for the example. I wasn’t aware that definitions could be changed like that. I thought of them more as immutable once defined.

---

<div class="post-metadata">

**Author:** ![Anton\_Hinneck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anton_hinneck/32/22716_2.png) [@Anton\_Hinneck](https://discourse.julialang.org/u/Anton_Hinneck)\
**Post date:** [July 21, 2023, 12:01pm UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/9 "2023-07-21T12:01:35Z")

</div>

Yes, that is exactly my use case. I need to write a plugins using c functions (defined in Julia) that interact with a shared library.

---

<div class="post-metadata">

**Author:** ![Anton\_Hinneck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anton_hinneck/32/22716_2.png) [@Anton\_Hinneck](https://discourse.julialang.org/u/Anton_Hinneck)\
**Post date:** [July 21, 2023, 12:04pm UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/10 "2023-07-21T12:04:35Z")

</div>

Thank you for bringing this up. I definitely see massive issues server-side exposing input to eval to a user - similar to SQL injection. Client-side it’s probably only additional overhead thats a downside, right?

---

<div class="post-metadata">

**Author:** ![Anton\_Hinneck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anton_hinneck/32/22716_2.png) [@Anton\_Hinneck](https://discourse.julialang.org/u/Anton_Hinneck)\
**Post date:** [July 21, 2023, 12:05pm UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/11 "2023-07-21T12:05:48Z")

</div>

I really like this approach. If i can create a c-callable function pointer off of an anonymous function, I will use it.

---

<div class="post-metadata">

**Author:** ![Anton\_Hinneck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anton_hinneck/32/22716_2.png) [@Anton\_Hinneck](https://discourse.julialang.org/u/Anton_Hinneck)\
**Post date:** [July 21, 2023, 12:07pm UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/12 "2023-07-21T12:07:39Z")

</div>

If i can create a C-callable function pointer using anonymous functions, I will probably use them. Is this possible?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 21, 2023, 12:13pm UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/13 "2023-07-21T12:13:33Z")

</div>

> [@Anton\_Hinneck](#):
>
> If i can create a C-callable function pointer using anonymous functions, I will probably use them. Is this possible?

Yes, as long as you’re not on ARM or PowerPC: [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Closure-cfunctions)

If the C API is properly designed, however, this should not be needed — properly designed C APIs that accept a function pointer should also take a `void*` argument that is passed through to your function, in order to simulate closures (as otherwise the code cannot be re-entrant). In this case you can use the `void*` to pass through an anonymous function to a thin static wrapper routine, as e.g. [explained here](https://julialang.org/blog/2013/05/callback/#the_problem_with_closures) (somewhat old, but the basic principle still holds).

(And if the C API is not re-entrant, then you might as well make your Julia callback non-re-entrant too. Just write a thin static wrapper that calls your “actual” callback stashed in a global.)

---

<div class="post-metadata">

**Author:** ![Anton\_Hinneck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anton_hinneck/32/22716_2.png) [@Anton\_Hinneck](https://discourse.julialang.org/u/Anton_Hinneck)\
**Post date:** [July 21, 2023, 12:30pm UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/14 "2023-07-21T12:30:58Z")

</div>

Yes, I just tried to make a concise problem statement. In fact, a slight change may be required in any of the generated functions’ bodies - but not enough to make writing them by hand efficient.

Thanks for mentioning the performance hit. This is indeed a concern. In my case only 2-3 sets may have to be generated at the beginning of execution - which should not be too bad.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 21, 2023, 12:48pm UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/15 "2023-07-21T12:48:55Z")

</div>

> [@Anton\_Hinneck](#):
>
> Client-side it’s probably only additional overhead thats a downside, right?

It’s just bad practise all round.

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [July 21, 2023, 1:02pm UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/16 "2023-07-21T13:02:30Z")

</div>

You didn’t go much into what functions you might want to generate (and have slight body changes at runtime).

So just a wild shot here: check out the [DynamicExpressions.jl](https://github.com/SymbolicML/DynamicExpressions.jl) package.

You might use the package to design the runtime-changeable parts of your functions and store the dynamic expressions in a `Ref` (so you don’t need to recompile the calling function).

In that way, you might be able to have functions that behave like the function body was changed (e.g., operating on the `Ref` containing the dynamic expressions from outside the function) without actually having a function body change (and without performance hit).

This might not be out of the box 100% compatible with your use case - but if performance is paramount, you can design your project around it.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 22, 2023, 12:07am UTC](https://discourse.julialang.org/t/programatic-function-and-variable-names/101861/17 "2023-07-22T00:07:06Z")

</div>

> [@Anton\_Hinneck](#):
>
> In fact, a slight change may be required in any of the generated functions’ bodies - but not enough to make writing them by hand efficient.

Any way to make that slight change a method argument or a field of a callable instance? Those won’t need you to generate multiple methods to compile separately, but if those changes mean different types then the same method will be compiled many ways, which doesn’t save much compilation time but does trim down the number of functions.
