# What's the best idiom here?

**URL:** <https://discourse.julialang.org/t/whats-the-best-idiom-here/63483>\
**Category:** General Usage\
**Created:** [June 24, 2021, 3:59am UTC](https://discourse.julialang.org/t/whats-the-best-idiom-here/63483 "2021-06-24T03:59:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)\
**Post date:** [June 24, 2021, 3:59am UTC](https://discourse.julialang.org/t/whats-the-best-idiom-here/63483/1 "2021-06-24T03:59:26Z")

</div>

Hello all

I have a project in which I am using variables to represent sets of local state. For example:

```nohighlight
    path::String
end
locf = LocalFile("/tmp/myfile")

```

I then want to give the user the opportunity to delete these bits of state via the API, so I have a function

```julia
function delete_file!(l::LocalFile)
???
end

```

The issue I’m having here - which might be a programming one or might be a conceptual one - is what to do with this variable when I delete the file. I was originally thinking to just set it to `nothing`, but I’m not actually sure that this is possible - it seems that you can mutate function parameters but not entirely re-assign them. A `clear!` function seems like it might have been appropriate but is no longer part of the language. Any suggestions much appreciated.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 24, 2021, 5:22am UTC](https://discourse.julialang.org/t/whats-the-best-idiom-here/63483/2 "2021-06-24T05:22:11Z")

</div>

Why do you want to delete the variable in the first place? It’s just a string, not the whole file as saved on disk. Once `locf` goes out of scope, the garbage collector will free the memory on its own.

Is there some more context that would be helpful here?

---

<div class="post-metadata">

**Author:** ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)\
**Post date:** [June 24, 2021, 6:06am UTC](https://discourse.julialang.org/t/whats-the-best-idiom-here/63483/3 "2021-06-24T06:06:33Z")

</div>

The thinking is that it then makes it clear to the user that the underlying resource (in this case, the file) is no longer present. For example, running delete\_file! again twice on the same variable would generate an error second time around, which is nice and clear.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 24, 2021, 6:12am UTC](https://discourse.julialang.org/t/whats-the-best-idiom-here/63483/4 "2021-06-24T06:12:52Z")

</div>

If you use [`rm`](https://docs.julialang.org/en/v1/base/file/#Base.Filesystem.rm), it will already error when the given file doesn’t exist:

```julia
julia> rm("this_file_does_not_exist")                                                 
ERROR: IOError: unlink("this_file_does_not_exist"): no such file or directory (ENOENT)
Stacktrace:                                                                           
[...]                                                                                 

```

In general, “unbinding” a variable is not possible.

---

<div class="post-metadata">

**Author:** ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)\
**Post date:** [June 24, 2021, 7:17am UTC](https://discourse.julialang.org/t/whats-the-best-idiom-here/63483/5 "2021-06-24T07:17:58Z")

</div>

Yes, that works reasonably well in that example (a local file). It’s still possible that the user may view the `LocalFile` struct in the interim, though, and not realize that it’s no longer valid. Ideally I think I would set it to `nothing`, but it seems like this isn’t possible. Thanks though.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 24, 2021, 8:23am UTC](https://discourse.julialang.org/t/whats-the-best-idiom-here/63483/6 "2021-06-24T08:23:42Z")

</div>

If you want to communicate to the user that they have already deleted the file, you can check for the existence explicitly with `isfile` or save that information as part of the struct.

If you’re concerned with persistent bindings in the REPL, I recommend putting more things into functions (which clean up their local variables after execution) and not having the variables linger in global scope in the first place.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 24, 2021, 8:52am UTC](https://discourse.julialang.org/t/whats-the-best-idiom-here/63483/7 "2021-06-24T08:52:18Z")

</div>

> [@harris-chris](#):
>
> For example, running delete\_file! again twice on the same variable would generate an error second time around, which is nice and clear.

Do you mean something like this?

```julia
function delete_file!(l::LocalFile)
    isempty(l.path) && error("File has already been deleted")
    [...]
    l.path = ""
end

```
