`UndefVarError` thrown in defined variable

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

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?

I am unable to reproduce the error with:

struct Struct
    Struct(i) = new(i, "$i")

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

Could you please provide a complete example.

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!