# Listing the names of local variables

**URL:** <https://discourse.julialang.org/t/listing-the-names-of-local-variables/9776>\
**Category:** New to Julia\
**Created:** [March 17, 2018, 10:07pm UTC](https://discourse.julialang.org/t/listing-the-names-of-local-variables/9776 "2018-03-17T22:07:05Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 6, 2018, 1:43am UTC](https://discourse.julialang.org/t/listing-the-names-of-local-variables/9776/19 "2018-04-06T01:43:14Z")

</div>

FWIW, this kind of functionality is often easier to implement than it looks. With [MacroTools](https://github.com/MikeInnes/MacroTools.jl#expression-walking):

```julia
using MacroTools: prewalk
using MacroTools

# Your example:
expr = quote
    function readfile(filename::String)
   a = Dict{String,Int}()
   buf = ""
   open(filename,"r") do ios
      while(!eof(ios))
         bud = strip(readline(ios))
         items = split(buf,"")
         a[items[1]] = length(items)
      end
   end
   return a
end
end

# Gather all local variables
vars = []
prewalk(expr) do x
    @match x begin
        (lhs_Symbol = b_) => push!(vars, lhs)
        for var_Symbol in _ __ end => push!(vars, var)
        ((args__,) -> _) => push!(vars, [splitarg(a)[1] for a in args]...)
    end
    x
end
vars

```

returns `[:a, :buf, :ios, :bud, :items]`. It’s by no means a complete solution, but I don’t think you’d need that many more lines to find all local variables in every function in a file. Then if you gather all variable uses with a similar strategy, you can detect that `:bud` is never used, and emit a warning…

---

_[View the full topic](https://discourse.julialang.org/t/listing-the-names-of-local-variables/9776)._
