# File operations and global variables

**URL:** <https://discourse.julialang.org/t/file-operations-and-global-variables/11995>\
**Category:** New to Julia\
**Created:** [June 27, 2018, 2:27pm UTC](https://discourse.julialang.org/t/file-operations-and-global-variables/11995 "2018-06-27T14:27:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckoe-bccms](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ckoe-bccms/32/1816_2.png) [@ckoe-bccms](https://discourse.julialang.org/u/ckoe-bccms)\
**Post date:** [June 27, 2018, 2:27pm UTC](https://discourse.julialang.org/t/file-operations-and-global-variables/11995/1 "2018-06-27T14:27:58Z")

</div>

Hello everybody,

I came upon this behaviour in the current 0.70-beta which surprised me a bit:

```julia
julia> f=open("basin.txt","r")
IOStream(<file basin.txt>)

julia> close(f)

julia> for i=1:1
       f=open("basin.txt","r")
       close(f)
       end
┌ Warning: Deprecated syntax `implicit assignment to global variable `f``.
│ Use `global f` instead.
└ @ nothing none:0

julia> typeof(f)
IOStream

```

Is this just that the depreciation warning is triggered by accident (if was some “normal” variable it would of course be correct) or should I really use global or rename throw-away variables in 1.0 ? Or asking the more basic question: Is there a better way to introduce throw-away temporary variables ?

Also, I would have expected that f is no longer in existence (garbage collected ?) after closing.

Most file IO probably happens in functions and not in main scope, so this is probably quite some niche case.

Best Regards

Christof

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 27, 2018, 2:41pm UTC](https://discourse.julialang.org/t/file-operations-and-global-variables/11995/2 "2018-06-27T14:41:40Z")

</div>

It’s unrelated to file operation.

> [@ckoe-bccms](#):
>
> if was some “normal” variable it would of course be correct

No, depending on what you meant by correct. The warning will still be there.

> [@ckoe-bccms](#):
>
> should I really use global or rename throw-away variables in 1.0

You are using global variable. The warning is just telling you that you won’t be using global variable implicitly anymore.

> [@ckoe-bccms](#):
>
> Is there a better way to introduce throw-away temporary variables ?

You are not using local variable. In 1.0 you will though.

> [@ckoe-bccms](#):
>
> Also, I would have expected that f is no longer in existence (garbage collected ?) after closing.

No, closing has nothing to do with GC. And it’s also the other way around. The object will be gc’d after it doesn’t exist.

---

<div class="post-metadata">

**Author:** ![ckoe-bccms](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ckoe-bccms/32/1816_2.png) [@ckoe-bccms](https://discourse.julialang.org/u/ckoe-bccms)\
**Post date:** [June 27, 2018, 3:14pm UTC](https://discourse.julialang.org/t/file-operations-and-global-variables/11995/3 "2018-06-27T15:14:38Z")

</div>

Hello,

> It’s unrelated to file operation.

I am perfectly aware of that.

> No, depending on what you meant by correct. The warning will still be there.

With “correct” I referred to the case of a (supposedly performance critical) say Float64 variable where this warning makes obvious sense. What surprised me was that I get this for a variable of type IOStream which is usually not performance critical.  
Or would this still trip the compilers optimizer regarding everything else in the loop ?

> You are not using local variable. In 1.0 you will though.

OK, I see now that I might use the local keyword. Did not think of searching for the opposite of global.

Best Regards

Christof

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [June 27, 2018, 3:29pm UTC](https://discourse.julialang.org/t/file-operations-and-global-variables/11995/4 "2018-06-27T15:29:34Z")

</div>

> [@ckoe-bccms](#):
>
> What surprised me was that I get this for a variable of type IOStream which is usually not performance critical.

Would you expect scoping behavior of variables to be different depending on what kind of object the variable refers to? I’m not sure that’s even possible and would definitely be… weird.

---

<div class="post-metadata">

**Author:** ![ckoe-bccms](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ckoe-bccms/32/1816_2.png) [@ckoe-bccms](https://discourse.julialang.org/u/ckoe-bccms)\
**Post date:** [June 27, 2018, 3:46pm UTC](https://discourse.julialang.org/t/file-operations-and-global-variables/11995/5 "2018-06-27T15:46:32Z")

</div>

Hello,

certainly different scoping behaviour based on type would be most dangerous. I agree.

I think my surprise boils down to the (obviously wrong) assumption that the variable “f” was gone after the close(f). It is no longer needed after close(f) and ad hoc I would assume it could no longer be used for anything else without completely overwriting the data structure it represents ?

If it would indeed no longer exist than re-using f again inside the loop should be harmless (there would be no longer a f in global scope, right ?) and therefore the depreciation warning would make no sense.

So, at which point would a variable vanish after the close(f) ? I do not yet understand why f is kept around after the close(f), I guess.

Thank you all for your replies !

Best Regards

Christof

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [June 27, 2018, 3:53pm UTC](https://discourse.julialang.org/t/file-operations-and-global-variables/11995/6 "2018-06-27T15:53:11Z")

</div>

`close` is just a function, it can’t do anything about the binding of the variable `f`, all it can do is modify the object referred to by `f`—it continues to be a file handle but after the `close(f)` call, it’s a closed file handle instead of an open one.

---

<div class="post-metadata">

**Author:** ![ckoe-bccms](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ckoe-bccms/32/1816_2.png) [@ckoe-bccms](https://discourse.julialang.org/u/ckoe-bccms)\
**Post date:** [June 27, 2018, 3:58pm UTC](https://discourse.julialang.org/t/file-operations-and-global-variables/11995/7 "2018-06-27T15:58:24Z")

</div>

Hello,

right ! For me conceptually f was somehow identical with the “accesability” of the file and ceased to exist together with the file access.

Thank you all !

Best Regards

Christof
