# Way to treat function as symbol using boolean flag?

**URL:** <https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 17, 2018, 9:16am UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384 "2018-04-17T09:16:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [April 17, 2018, 9:16am UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/1 "2018-04-17T09:16:42Z")

</div>

Let’s say you have a bunch of coefficients, i.e.

```julia
function K_PC(cur_blah::AbstractBlah)
  cur_K = K_law(cur_blah)

  cur_K *= K_G(cur_blah) ^ 2
  cur_K *= 1.273

  cur_K /= cur_blah.eta_T

  cur_K
end

```

However, you’re using `SymPy` and want to enable some sort of `symbolic mode`

// that doesn’t evaluate any of the values (i.e. leaves the output pretty)

Is there a way to know the name of `K_PC` inside itself?

* * *

This would allow you to short-circuit the function at the top with:

> `return SymPy.symbols(<name_of_function>)`

// where `<name_of_function>` would be `K_PC` in this example

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [April 17, 2018, 9:45am UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/2 "2018-04-17T09:45:45Z")

</div>

You could write a macro

```julia
@name function K_PC(cur_blah::AbstractBlah)
  ...
end

```

That expands to something like

```julia
function K_PC(cur_blah::AbstractBlah)
    __FUNCTION_NAME__ = :K_PC
  ...
end

```

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [April 17, 2018, 11:58am UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/3 "2018-04-17T11:58:07Z")

</div>

I think `string(stacktrace()[1].func)` will return the function’s own name (and `[2]` the function calling it, etc).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 17, 2018, 12:07pm UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/4 "2018-04-17T12:07:47Z")

</div>

An alternative solution if you merely need it for DRY and want to avoid macros is to define your operations as types, then make them callable:

```julia
abstract type AbstractOp end

struct Op1 <: AbstractOp end

function (op::Op1)(arg)
    Symbol(typeof(op))
end

```

Also, you can define a

```julia
sympyname(::Op1) = :op1

```

and thus decouple the two namespaces.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [April 18, 2018, 9:22pm UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/5 "2018-04-18T21:22:57Z")

</div>

Pretty cool solutions across the board! Thanks for your help.

* * *

The code I ended up on is:

```julia
macro symbol_func(cur_expr::Expr)
  @assert cur_expr.head == :function

  cur_call = cur_expr.args[1]
  cur_func_name = cur_call.args[1]
  cur_main_var = cur_call.args[2].args[1]

  cur_param = Expr(:kw, Expr(:(::), :is_direct_call, :Bool), false)

  cur_sub_expr = parse("""
    $(cur_main_var).is_symbolic && !is_direct_call &&
      return symbols("$(cur_func_name)")
  """)

  push!(cur_expr.args[1].args, cur_param)
  unshift!(cur_expr.args[2].args, cur_sub_expr)

  cur_expr
end

```

With the initial code now being:

```julia
@symbol_func function K_PC(cur_blah::AbstractBlah)
  cur_K = K_law(cur_blah)

  cur_K *= K_G(cur_blah) ^ 2
  cur_K *= 1.273

  cur_K /= cur_blah.eta_T

  cur_K
end

```

---

<div class="post-metadata">

**Author:** ![samoconnor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samoconnor/32/1802_2.png) [@samoconnor](https://discourse.julialang.org/u/samoconnor)\
**Post date:** [April 18, 2018, 10:24pm UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/6 "2018-04-18T22:24:23Z")

</div>

> [@improbable22](#):
>
> I think string(stacktrace()[1].func) will return the function’s own name (and [2] the function calling it, etc).

I was using this approach for a [precondition marco](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/debug.jl#L46-L4) that wants to use the function name in an error message. It turned out [not to be generally reliable](https://github.com/JuliaWeb/HTTP.jl/issues/148#issuecomment-358457791). Possibly because [issue #19979](https://github.com/JuliaLang/julia/issues/19979) means that stack traces sometimes get corrupted when `try/catch` is used? Possibly due to inlining?

(Another variation of a precondition macro that needs the function name: [https://github.com/JuliaLang/julia/pull/15495](https://github.com/JuliaLang/julia/pull/15495))

---

<div class="post-metadata">

**Author:** ![samoconnor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samoconnor/32/1802_2.png) [@samoconnor](https://discourse.julialang.org/u/samoconnor)\
**Post date:** [April 18, 2018, 10:28pm UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/7 "2018-04-18T22:28:40Z")

</div>

@srp asked for `@ __FUNCTION__ ` a while back.

This issue should be re-opened because none of the referenced issues have implemented anything like `@ __FUNCTION__ `

[https://github.com/JuliaLang/julia/issues/6733](https://github.com/JuliaLang/julia/issues/6733)

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [April 18, 2018, 10:34pm UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/8 "2018-04-18T22:34:48Z")

</div>

Forgot to post a picture of the use case!

(left is a paper. right is code. and they match!)

* * *

 ![25%20PM](https://global.discourse-cdn.com/julialang/original/3X/4/b/4bd2c419ea3536ce277a451a1db6199528820067.png)

**update:** already found 2 bugs in some code using this 😬

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [April 19, 2018, 1:07am UTC](https://discourse.julialang.org/t/way-to-treat-function-as-symbol-using-boolean-flag/10384/9 "2018-04-19T01:07:31Z")

</div>

I’d also love to see `@ __FUNCTION__ ` added.
