# Implicit cast of a struct into a tuple

**URL:** <https://discourse.julialang.org/t/implicit-cast-of-a-struct-into-a-tuple/77014>\
**Category:** New to Julia\
**Created:** [February 24, 2022, 9:27am UTC](https://discourse.julialang.org/t/implicit-cast-of-a-struct-into-a-tuple/77014 "2022-02-24T09:27:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![whatsthecraic](https://avatars.discourse-cdn.com/v4/letter/w/7bcc69/32.png) [@whatsthecraic](https://discourse.julialang.org/u/whatsthecraic)\
**Post date:** [February 24, 2022, 9:27am UTC](https://discourse.julialang.org/t/implicit-cast-of-a-struct-into-a-tuple/77014/1 "2022-02-24T09:27:58Z")

</div>

Hello!

Is there a way to define an implicit cast of a struct into a tuple, such that the following syntax becomes valid:

```julia
struct T ; x::String ; y::Integer ; end

t = T("x", 1)
a, b = t

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 24, 2022, 10:29am UTC](https://discourse.julialang.org/t/implicit-cast-of-a-struct-into-a-tuple/77014/2 "2022-02-24T10:29:25Z")

</div>

Not sure if `a, b = t` is possible, I’ve never seen customizing assignments, but you can put the fields of a struct instance into a tuple:

```julia
function struct2tuple(inst)
  numfields = fieldcount(typeof(inst))
  ntuple(i -> getfield(inst, i), numfields) 
end

a, b = struct2tuple(t) # a::String, b::Int64

```

Note that the method is type-unstable if the struct has any abstract field types. `Tuple{String, Integer}` is an abstract type because Tuples can’t hold pointers like a struct can. `struct2tuple` returns the concrete type `Tuple{String, Int64}`.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [February 24, 2022, 10:33am UTC](https://discourse.julialang.org/t/implicit-cast-of-a-struct-into-a-tuple/77014/3 "2022-02-24T10:33:03Z")

</div>

For destructuring to work, you “simply” need to define how to iterate over instances of your struct. Note that this has the (potentially unintended) side effect that one can then iterate on fields of your struct in all other contexts as well.

Since you want iteration on your struct to behave like iteration over the tuple of field values, one way to do that is to borrow the implementation of iteration on tuples:

```julia
julia> struct T
           x::String
           y::Integer
       end

# calling `iterate` on t is like calling `iterate` on the tuple `(t.x, t.y)`
julia> Base.iterate(t::T, args...) = iterate((t.x, t.y), args...)

julia> a, b = T("a", 1)
T("a", 1)

julia> a, b
("a", 1)

```

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [February 24, 2022, 12:41pm UTC](https://discourse.julialang.org/t/implicit-cast-of-a-struct-into-a-tuple/77014/5 "2022-02-24T12:41:57Z")

</div>

There is [UnPack.jl](https://github.com/mauro3/UnPack.jl) package which allows to do this

```julia
@unpack x, y = t

```

Though, the names in the tuple should match the fields in the struct.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 24, 2022, 4:17pm UTC](https://discourse.julialang.org/t/implicit-cast-of-a-struct-into-a-tuple/77014/6 "2022-02-24T16:17:13Z")

</div>

This functionality is in base now:

```julia
(; x, y) = t

```

---

<div class="post-metadata">

**Author:** ![dcnorris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcnorris/32/44845_2.png) [@dcnorris](https://discourse.julialang.org/u/dcnorris)\
**Post date:** [April 14, 2025, 9:19am UTC](https://discourse.julialang.org/t/implicit-cast-of-a-struct-into-a-tuple/77014/7 "2025-04-14T09:19:11Z")

</div>

Link to manual, under heading _Property destructuring_:

> **[Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Property-destructuring)**
>
> Documentation for The Julia Language.
