# \`UndefVarError\` thrown in defined variable

**URL:** https://discourse.julialang.org/t/undefvarerror-thrown-in-defined-variable/112779
**Category:** New to Julia
**Tags:** question
**Created:** [April 10, 2024, 2:43pm UTC](https://discourse.julialang.org/t/undefvarerror-thrown-in-defined-variable/112779 "2024-04-10T14:43:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![azeredo-e](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azeredo-e/32/208402_2.png) [@azeredo-e](https://discourse.julialang.org/u/azeredo-e)
#### Post date: [April 10, 2024, 2:43pm UTC](https://discourse.julialang.org/t/undefvarerror-thrown-in-defined-variable/112779/1 "2024-04-10T14:43:59Z")

</div>

I have a code that is throwing an `UndefVarError` even though the variable exists.

```julia
for code in (Struct(i) for i in Tuple(...))
    @assert typeof(code.name) <: AbstractString # Error: `code` not defined

```

`Struct()` is an imutable Struct with two fields: name and value. And the tuple is just a list of ints. I use a inner constructor for creating the name and value field from just one input, `i`.

When I tested with `@isdefined code` or `@isdefined code.name`, or even, `typeof(code.name)`, it goes fine, it prints to the terminal just fine showing that the object does in fact exists, but it always throws an error in the assert.

What is going on here?

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [April 10, 2024, 3:53pm UTC](https://discourse.julialang.org/t/undefvarerror-thrown-in-defined-variable/112779/2 "2024-04-10T15:53:50Z")

</div>

I am unable to reproduce the error with:

```julia
struct Struct
    i::Int
    name::String
    Struct(i) = new(i, "$i")
end

for code in (Struct(i) for i in (1,2,3))
    @assert typeof(code.name) <: AbstractString
end

```

Could you please provide a complete example.

---

<div class="post-metadata">

### Author: ![azeredo-e](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azeredo-e/32/208402_2.png) [@azeredo-e](https://discourse.julialang.org/u/azeredo-e)
#### Post date: [April 10, 2024, 4:34pm UTC](https://discourse.julialang.org/t/undefvarerror-thrown-in-defined-variable/112779/3 "2024-04-10T16:34:39Z")

</div>

I’m so sorry, I interpreted the error message wrongly and the error was throwing outside of the scope of the `for` loop, that’s why it was showing an error.

But thank you so much for the help!
