# Proposal to add method \`Base.read(io::IO, ::Type{T}) where T \<: IO\`

**URL:** <https://discourse.julialang.org/t/proposal-to-add-method-base-read-io-io-type-t-where-t-io/137044>\
**Category:** Internals & Design\
**Tags:** base, io\
**Created:** [May 8, 2026, 3:38pm UTC](https://discourse.julialang.org/t/proposal-to-add-method-base-read-io-io-type-t-where-t-io/137044 "2026-05-08T15:38:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [May 8, 2026, 3:38pm UTC](https://discourse.julialang.org/t/proposal-to-add-method-base-read-io-io-type-t-where-t-io/137044/1 "2026-05-08T15:38:30Z")

</div>

Currently `Base.read()` does support

```julia
read(src, String)
read(src, Int32)

```

but the following signatures are failing

```julia
read(src, IOBuffer)
read(src, Base.SecretBuffer)

```

What do you think about introducing

```julia
"""
    read(io::IO, T::Type{<:IO}) -> T

Read the contents of `io` into a new instance of `T` and return it
rewound to the start. `T` must support a zero-argument constructor
and be seekable.

# Examples
# io = IOBuffer("Hello World")
# buf = read(io, IOBuffer)
# sb = read(open("/run/secrets/token"), SecretBuffer)
"""
function Base.read(io::IO, ::Type{T}) where T <: IO
    dst = T()
    write(dst, io)
    return seekstart(dst)
end

```

or shorter

```julia
Base.read(io::IO, ::Type{T}) where T <: IO =
    (dst = T(); write(dst, io); seekstart(dst))

```

I am aware that the signature is quite general and that the method will fail if a buffer doesn’t support the constructor call or isn’t seekable. But on the other hand the meaning of this signature is quite straight forward and probably meaningless for buffers that don’t fulfill that condition.  
Would be happy to receive feedback.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [May 8, 2026, 3:49pm UTC](https://discourse.julialang.org/t/proposal-to-add-method-base-read-io-io-type-t-where-t-io/137044/2 "2026-05-08T15:49:01Z")

</div>

the docstring says

```julia-auto
  read(io::IO, T)

  Read a single value of type T from io

```

so I’d be expecting `read(io::IO, ::Type{IO})` to read a single `IO` object off of `IO`. the functionality you propose I’d probably expect to be spelled more like `read!(dst::IO, src::IO)` ?

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [May 8, 2026, 3:53pm UTC](https://discourse.julialang.org/t/proposal-to-add-method-base-read-io-io-type-t-where-t-io/137044/3 "2026-05-08T15:53:35Z")

</div>

Your proposed functionality ist already covered by `write(::IO, ::IO)`, I think.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [May 8, 2026, 3:59pm UTC](https://discourse.julialang.org/t/proposal-to-add-method-base-read-io-io-type-t-where-t-io/137044/4 "2026-05-08T15:59:51Z")

</div>

I’m not sure about that actually

```julia-auto
julia> io = IOBuffer(); io2 = IOBuffer();

julia> write(io, "abcde")
5

julia> write(io, io2)
0

julia> write(io2, io)
0

```

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [May 8, 2026, 4:11pm UTC](https://discourse.julialang.org/t/proposal-to-add-method-base-read-io-io-type-t-where-t-io/137044/5 "2026-05-08T16:11:12Z")

</div>

You need to rewind the buffer with `seekstart(io)`
