# read methods for composite types

**URL:** <https://discourse.julialang.org/t/read-methods-for-composite-types/10262>\
**Category:** Internals & Design\
**Created:** [April 10, 2018, 12:30pm UTC](https://discourse.julialang.org/t/read-methods-for-composite-types/10262 "2018-04-10T12:30:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![miromarszal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miromarszal/32/6721_2.png) [@miromarszal](https://discourse.julialang.org/u/miromarszal)\
**Post date:** [April 10, 2018, 12:30pm UTC](https://discourse.julialang.org/t/read-methods-for-composite-types/10262/1 "2018-04-10T12:30:40Z")

</div>

I wasn’t sure whether it’s a bug or intended behaviour, so I’m posting it here.

I run into a possible inconsistency in read methods when applied to a composite type. See the following example.

Let’s create a simple composite type that stores an integer

```julia
julia> struct myint
           data::Int
       end

```

and a stream from which we would like to read the value

```julia
julia> stream = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> write(stream, 1)
8

```

Now, reading directly into `Int` works fine as expected

```julia
julia> seekstart(stream)
julia> read(stream, Int)
1

```

but reading into `myint` fails

```julia
julia> seekstart(stream)
julia> read(stream, myint)
ERROR: MethodError: Cannot `convert` an object of type Type{myint} to an object of type Array{UInt8,1}
This may have arisen from a call to the constructor Array{UInt8,1}(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] read(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Type{T} where T) at ./io.jl:528

```

Curiously, reading into an array of `myint` works!

```julia
julia> seekstart(stream)
julia> read(stream, myint, 1)
1-element Array{myint,1}:
 myint(1)

```

Now, is it a bug or a desired behaviour? I would expect `read` either to succeed or fail in both cases.
