# Generic generation of functions

**URL:** <https://discourse.julialang.org/t/generic-generation-of-functions/127968>\
**Category:** General Usage\
**Created:** [April 11, 2025, 9:58am UTC](https://discourse.julialang.org/t/generic-generation-of-functions/127968 "2025-04-11T09:58:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Lincke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daniel_lincke/32/206206_2.png) [@Daniel\_Lincke](https://discourse.julialang.org/u/Daniel_Lincke)\
**Post date:** [April 11, 2025, 9:58am UTC](https://discourse.julialang.org/t/generic-generation-of-functions/127968/1 "2025-04-11T09:58:52Z")

</div>

Hi,

imagine the following situation: I have a lot of function that operate on some struct and get a symbol as input (maybe function that do something with a dataframe):

```julia
function doA(... , s::Symbol)
function doB(... , s::Symbol)
function doC(... , s::Symbol)
...

```

Note that doA, doB, … might have different (number of) arguments, only the last argument is always of type Symbol. I would like to have for each function a version that operates on Strings. I could implement each of these functions

```julia
function doA(... , s::String) = doA(..., Symbol(s))
function doB(... , s::String) = doB(..., Symbol(s))
function doC(... , s::String) = doC(..., Symbol(s))
...

```

This leads to a lot of boilerplate code which I would like to avoid. Is there a standard method to deal with such situations?

Thanks!

---

<div class="post-metadata">

**Author:** ![yolhan\_mannes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yolhan_mannes/32/220485_2.png) [@yolhan\_mannes](https://discourse.julialang.org/u/yolhan_mannes)\
**Post date:** [April 11, 2025, 10:11am UTC](https://discourse.julialang.org/t/generic-generation-of-functions/127968/2 "2025-04-11T10:11:21Z")

</div>

This may be a bit ugly but works, it does switch ordering though

```julia
doA(a,b,c,S::Symbol) = S
doB(a,S::Symbol) = S
doC(a,b,S::Symbol) = S
struct StrFun{F<:Function}
           f::F
       end
(fstr::StrFun)(s::String,args...) = fstr.f(args...,Symbol(s))
StrFun(doA)("abc",1,2,3)

```

unless you really want this to be methods

---

<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:** [April 11, 2025, 12:45pm UTC](https://discourse.julialang.org/t/generic-generation-of-functions/127968/3 "2025-04-11T12:45:44Z")

</div>

> [@Daniel\_Lincke](#):
>
> `doA(..., Symbol(s))`

If `s::String` or any other input is just for conversion to `Symbol` before forwarding to the `s::Symbol` implementation, then why not call this directly? The only reason I would call `doA(..., s)` is if I expected the type of `s` to change what methods are dispatched.

If there _are_ more specific `s::String` methods like that and the `Symbol` conversion is just the fallback, then this is justified. Duplicate method signatures don’t work in a package, so if you might want that then you’d have to carefully avoid it, especially with metaprogramming. Personally, I’d do so by changing your existing methods to `sl::SymbolLike`, internally convert `s = Symbol(sl)`, and name the update-able supertype `const SymbolLike = Union{Symbol, String}`. Then any more specific `s::String` methods won’t be duplicates.
