# Why are stdout, stderr etc. not const global variables?

**URL:** <https://discourse.julialang.org/t/why-are-stdout-stderr-etc-not-const-global-variables/98721>\
**Category:** Internals & Design\
**Created:** [May 12, 2023, 8:05am UTC](https://discourse.julialang.org/t/why-are-stdout-stderr-etc-not-const-global-variables/98721 "2023-05-12T08:05:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [May 12, 2023, 8:05am UTC](https://discourse.julialang.org/t/why-are-stdout-stderr-etc-not-const-global-variables/98721/1 "2023-05-12T08:05:54Z")

</div>

I can overwrite their values (in Julia 1.9.0):

```julia
julia> Base.stderr = Base.stdout
Base.TTY(RawFD(13) open, 0 bytes waiting)

```

So whenever I write  
`print(stderr, "some message")`  
I’m using a non-const global variable from `Base`, which is not ideal for performance?

I found this out when I was trying JET.jl. The `@report_opt` tool complained about “runtime dispatch” for every line of my code that printed to `stderr`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 12, 2023, 12:25pm UTC](https://discourse.julialang.org/t/why-are-stdout-stderr-etc-not-const-global-variables/98721/2 "2023-05-12T12:25:16Z")

</div>

They can’t be `const` because you can change them (with [`redirect_stdout`](https://docs.julialang.org/en/v1/base/io-network/#Base.redirect_stdout) etc).

Typically, writing to `stdout` and `stderr` is not going to be performance critical, so the overhead of dynamic dispatch isn’t going to matter.

If you do have performance-critical I/O, it should generally [be in a function](https://docs.julialang.org/en/v1/manual/performance-tips/) that takes an `io::IO` argument so that it can write to an arbitrary stream (not just to `stdout`). In this case, the cost of dynamic dispatch is paid only once, when you call that function, not on every `print(io, ...)` call within the function.
