# Alias a struct using it's own field

**URL:** <https://discourse.julialang.org/t/alias-a-struct-using-its-own-field/83990>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [July 9, 2022, 2:57pm UTC](https://discourse.julialang.org/t/alias-a-struct-using-its-own-field/83990 "2022-07-09T14:57:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![moataz-sabry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moataz-sabry/32/36856_2.png) [@moataz-sabry](https://discourse.julialang.org/u/moataz-sabry)\
**Post date:** [July 9, 2022, 2:57pm UTC](https://discourse.julialang.org/t/alias-a-struct-using-its-own-field/83990/1 "2022-07-09T14:57:30Z")

</div>

let’s say I have the following struct:

```julia
struct Dog
    name
    age
end

```

then I wanted to create an array of dogs like the following:

```julia
array = [Dog(:Jack, 2), Dog(:Max, 3)]

2-element Vector{Dog}:
 Dog(:Jack, 2)
 Dog(:Max, 3)

```

is it possible to alias a struct with it’s field; in this case, the field “name” so that I would get something like this:

```julia
2-element Vector{Dog}:
 :Jack
 :Max

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 9, 2022, 3:01pm UTC](https://discourse.julialang.org/t/alias-a-struct-using-its-own-field/83990/2 "2022-07-09T15:01:21Z")

</div>

I don’t know what you mean by “alias” in this context, I presume you are not referring to accessing the `name` field like this:

```julia
julia> struct Dog
           name
           age
       end

julia> array = [Dog(:Jack, 2), Dog(:Max, 3)]
2-element Vector{Dog}:
 Dog(:Jack, 2)
 Dog(:Max, 3)

julia> getfield.(array, :name)
2-element Vector{Symbol}:
 :Jack
 :Max

```

?

---

<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:** [July 9, 2022, 3:05pm UTC](https://discourse.julialang.org/t/alias-a-struct-using-its-own-field/83990/3 "2022-07-09T15:05:38Z")

</div>

If what you mean by “alias” is to customize the way `Dog` instances are printed, then you can define a new, specific method for [`Base.show`](https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple%7BIO,%20Any,%20Any%7D). This is explained in mode details in the [Custom pretty-printing](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing) section of the manual

```julia
julia> struct Dog
           name
           age
       end

julia> array = [Dog(:Jack, 2), Dog(:Max, 3)]
2-element Vector{Dog}:
 Dog(:Jack, 2)
 Dog(:Max, 3)

julia> function Base.show(io::IO, mime::MIME"text/plain", dog::Dog)
           show(io, mime, dog.name)
       end

julia> array
2-element Vector{Dog}:
 :Jack
 :Max

```

---

<div class="post-metadata">

**Author:** ![moataz-sabry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moataz-sabry/32/36856_2.png) [@moataz-sabry](https://discourse.julialang.org/u/moataz-sabry)\
**Post date:** [July 9, 2022, 3:05pm UTC](https://discourse.julialang.org/t/alias-a-struct-using-its-own-field/83990/4 "2022-07-09T15:05:47Z")

</div>

If I am going to create an array of structs with say 5, 10 fields, it would look ugly. That’s why I wanted to represent each struct with the field name.

---

<div class="post-metadata">

**Author:** ![moataz-sabry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moataz-sabry/32/36856_2.png) [@moataz-sabry](https://discourse.julialang.org/u/moataz-sabry)\
**Post date:** [July 9, 2022, 3:08pm UTC](https://discourse.julialang.org/t/alias-a-struct-using-its-own-field/83990/5 "2022-07-09T15:08:39Z")

</div>

thanks, that’s what I was looking for!
