# Can I use \`@eval\` to dynamically define methods for dispatch on \`Val\`s?

**URL:** https://discourse.julialang.org/t/can-i-use-eval-to-dynamically-define-methods-for-dispatch-on-val-s/68468
**Category:** General Usage
**Tags:** question, metaprogramming
**Created:** [September 20, 2021, 2:45pm UTC](https://discourse.julialang.org/t/can-i-use-eval-to-dynamically-define-methods-for-dispatch-on-val-s/68468 "2021-09-20T14:45:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)
#### Post date: [September 20, 2021, 2:45pm UTC](https://discourse.julialang.org/t/can-i-use-eval-to-dynamically-define-methods-for-dispatch-on-val-s/68468/1 "2021-09-20T14:45:31Z")

</div>

Hi all,

I’m trying to dynamically define methods for a function to dispatch on specific names (symbols/strings) which seems to work outside a loop, but not inside a loop.

Can someone explain how I may be mis-applying the metaprogramming functionality of Julia here?

```julia-repl
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> for name in [:aye, :bee, :cee]
           @eval Main begin
               function fcn(::Val{name})
                   println(name)
               end
           end
       end
ERROR: UndefVarError: name not defined
Stacktrace:
 [1] top-level scope
   @ REPL[1]:3
 [2] eval(m::Module, e::Any)
   @ Core .\boot.jl:360
 [3] top-level scope
   @ REPL[1]:2

julia> new_name = :dee
:dee

julia> @eval Main begin
           function gcn(::Val{new_name})
               println(new_name)
           end
       end
gcn (generic function with 1 method)

julia> gcn(Val(:dee))
dee

```

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [September 20, 2021, 2:48pm UTC](https://discourse.julialang.org/t/can-i-use-eval-to-dynamically-define-methods-for-dispatch-on-val-s/68468/2 "2021-09-20T14:48:09Z")

</div>

You need to escape `name`, so

```julia
for name in [:aye, :bee, :cee]
    @eval Main begin
        function fcn(::Val{$name})
            println($name)
        end
    end
end

```

Otherwise it is trying to look up `name` when you run the method instead of when you define it.

---

<div class="post-metadata">

### Author: ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)
#### Post date: [September 22, 2021, 12:37am UTC](https://discourse.julialang.org/t/can-i-use-eval-to-dynamically-define-methods-for-dispatch-on-val-s/68468/3 "2021-09-22T00:37:52Z")

</div>

My apologies, I forgot to mention that I did try this as well, and it didn’t work.

```julia
julia> for name in [:aye, :bee, :cee]
    @eval Main begin
        function fcn(::Val{$name})
            println($name)
        end
    end
end

ERROR: UndefVarError: aye not defined
Stacktrace:
 [1] top-level scope
   @ REPL[1]:3
 [2] eval
   @ .\boot.jl:360 [inlined]
 [3] top-level scope
   @ .\REPL[1]:2

```

Thank you though! I figured it out and yeah what you suggested is one step closer, this is what works:

```julia
julia> for name in [:(:aye), :(:bee), :(:cee)]
    @eval Main begin
        function fcn(::Val{$name})
            println($name)
        end
    end
end

julia> fcn(Val(:aye))
aye

```

It’s just strange that is complains that `name` is undefined in my OP example.

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [September 22, 2021, 7:21am UTC](https://discourse.julialang.org/t/can-i-use-eval-to-dynamically-define-methods-for-dispatch-on-val-s/68468/4 "2021-09-22T07:21:57Z")

</div>

Another option is:

```julia
for name in [:aye, :bee, :cee]
    @eval Main begin
        function fcn(::Val{$(QuoteNode(name))})
            println($(QuoteNode(name)))
        end
    end
end

```
