# Stdout appears as type instable

**URL:** <https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481>\
**Category:** Performance\
**Created:** [October 27, 2023, 2:02pm UTC](https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481 "2023-10-27T14:02:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [October 27, 2023, 2:02pm UTC](https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481/1 "2023-10-27T14:02:28Z")

</div>

I made the following progress bar, but according to `@code_warntype` it is somewhat type instable. How can I make it type stable?

```julia
function progressbar(ntimes::S, current::S) where S<:Int
  val = current/ntimes
  val_rounded = round(S, val*10)
  bars = "█" ^ val_rounded
  remainder = " " ^ (10 - val_rounded)
  joined = bars*remainder
  percentage = round(val*100, digits=2)
  printstyled(stdout, "[$joined] $percentage% \r")
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/2/92dc9ba9ad5cbd39ccf9d6d0b11a911669771ccf.png)

P.S.: I do not want to use third-party packages for having a progress bar.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 27, 2023, 2:09pm UTC](https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481/2 "2023-10-27T14:09:25Z")

</div>

Does it have to be type stable? It’s just printing progress after all. You could typeassert it to `stdout::Base.TTY` I guess, but I don’t think you’d see all that much difference in performance, and it might then fail on systems where `stdout` is not a `TTY`.

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [October 27, 2023, 2:20pm UTC](https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481/3 "2023-10-27T14:20:31Z")

</div>

> Does it have to be type stable?

Yes, I want to embed this into my package. The package is type stable and I do not want to break the rule. As you said, the performance reduction might be negligible, but I don’t want to break the type stability of the package ☹  
By the way, Thank You So Much ❤

> [@baggepinnen](#):
>
> it might then fail on systems where `stdout` is not a `TTY`.

Yes… I do not do that.

---

<div class="post-metadata">

**Author:** ![ocp](https://avatars.discourse-cdn.com/v4/letter/o/c5a1d2/32.png) [@ocp](https://discourse.julialang.org/u/ocp)\
**Post date:** [October 27, 2023, 2:21pm UTC](https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481/4 "2023-10-27T14:21:37Z")

</div>

Will passing `stdout` as an argument work for you?

```julia
function progressbar(io, ntimes::S, current::S) where S<:Int
    val = current/ntimes
    val_rounded = round(S, val*10)
    bars = "█" ^ val_rounded
    remainder = " " ^ (10 - val_rounded)
    joined = bars*remainder
    percentage = round(val*100, digits=2)
    printstyled(io, "[$joined] $percentage% \r")
end

```

With that change `@code_warntype` no longer complains. It also gives you the ability to print to other IO types.

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [October 27, 2023, 2:25pm UTC](https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481/5 "2023-10-27T14:25:21Z")

</div>

> [@ocp](#):
>
> Will passing `stdout` as an argument work for you?

Yes, how is that going be a problem?

> [@ocp](#):
>
> With that change `@code_warntype` no longer complains. It also gives you the ability to print to other IO types.

Really cool. However, it prints the `IO` as well:

```julia
julia> for i ∈ 1:1000
         progressbar(IO, 1000, i)
         sleep(0.01)
       end
IO[██████████] 100.0% 

```

---

<div class="post-metadata">

**Author:** ![ocp](https://avatars.discourse-cdn.com/v4/letter/o/c5a1d2/32.png) [@ocp](https://discourse.julialang.org/u/ocp)\
**Post date:** [October 27, 2023, 2:27pm UTC](https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481/6 "2023-10-27T14:27:18Z")

</div>

Interesting that it prints the literal `IO`. To print to `stdout`, you should call it like this:

```julia
julia> progressbar(stdout, 1000, 500)
[█████] 50.0%

```

Or any even a file for example:

```julia
open("log.txt", "w") do io
    progressbar(io, 1000, 500)
end

```

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [October 27, 2023, 2:28pm UTC](https://discourse.julialang.org/t/stdout-appears-as-type-instable/105481/7 "2023-10-27T14:28:59Z")

</div>

Thank you so much. Great!
