# For T in (Float64,) vs let T=Float64

**URL:** <https://discourse.julialang.org/t/for-t-in-float64-vs-let-t-float64/77842>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 14, 2022, 2:06am UTC](https://discourse.julialang.org/t/for-t-in-float64-vs-let-t-float64/77842 "2022-03-14T02:06:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [March 14, 2022, 2:06am UTC](https://discourse.julialang.org/t/for-t-in-float64-vs-let-t-float64/77842/1 "2022-03-14T02:06:02Z")

</div>

I wonder why the first one in the following fails

```julia
for T in (Float64,)
    sizefloat(x::T) = sizeof(T)
    println(methods(sizefloat))
    println(sizefloat(3.4))
end

```

```julia
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] top-level scope

```

while the second one works:

```julia
let T=Float64
    sizefloat(x::T) = sizeof(T)
    println(methods(sizefloat))
    println(sizefloat(3.4))
end

```

```julia
# 1 method:
[1] (::var"#sizefloat#31")(x::Float64) in Main at E:\work\julia\mytest.jl:9
8

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 14, 2022, 3:13am UTC](https://discourse.julialang.org/t/for-t-in-float64-vs-let-t-float64/77842/2 "2022-03-14T03:13:01Z")

</div>

Hmm, that does seem inconsistent. I recognize the error. That’s what happens when you leave out the `where T` after `sizefloat(x::T)`, but in this case T is bound to `Float64` and so it seems like it should work the same as it does in the `let` block. Both the `for` loop and the `let` block introduce a local scope with a local variable `T` bound to `Float64`, so I’m not sure why they behave differently… 🤔

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 14, 2022, 5:50am UTC](https://discourse.julialang.org/t/for-t-in-float64-vs-let-t-float64/77842/3 "2022-03-14T05:50:14Z")

</div>

This seems similar to the question that came up in this thread: [Can a function return another function?](https://discourse.julialang.org/t/can-a-function-return-another-function/77806)

Apparently, it’s considered a bug or unwanted limitation. But I don’t really see why this _should_ work. Dynamically creating named functions at runtime seems unnatural to me.

Isn’t this metaprogramming, that should require a macro? Or, if you don’t really need a globally defined name, make an anonymous function.

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [March 14, 2022, 6:26am UTC](https://discourse.julialang.org/t/for-t-in-float64-vs-let-t-float64/77842/4 "2022-03-14T06:26:23Z")

</div>

It’s purely out of curiosity. There is a code snippet in Revise documentation:

```julia
for T in (Float16, Float32, Float64)
    @eval sizefloat(x::$T) = sizeof($T)
end

```

Then, I had this thought for a moment; the following should do the same:

```julia
for T in (Float16, Float32, Float64)
    global sizefloat(x::T) = sizeof(T)
end

```

It indeed works. Then I tried removing `global` keyword(I sometimes play with the language like that and it helps me understand the subject better.) When the code failed without `global` then I tried replacing `for` with `let’. 🙂

Thank you for the related topic!
