# Vocabulary when using NamedTuples and Structs

**URL:** <https://discourse.julialang.org/t/vocabulary-when-using-namedtuples-and-structs/56960>\
**Category:** General Usage\
**Created:** [March 11, 2021, 4:55pm UTC](https://discourse.julialang.org/t/vocabulary-when-using-namedtuples-and-structs/56960 "2021-03-11T16:55:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [March 11, 2021, 4:55pm UTC](https://discourse.julialang.org/t/vocabulary-when-using-namedtuples-and-structs/56960/1 "2021-03-11T16:55:55Z")

</div>

This may seem like an odd post, but bare with me here.

I’m assuming most, if not all of us here had some experience with OO languages, so it’s normal to hear terms like object, has-a, or is-a.

1. When you use a `NamedTuple`, is it correct to say that we have an object?

```julia
Fruit = @NamedTuple(name::String,Type::String, color::String)
ambrosia::Fruit = (name = "Ambrosia", Type="Apple" color = "Red")

```

Is it correct to say `ambrosia` is a object of Fruit?

1. If we really wanted to go deeper, fruits obviously have certain attributes, name, colour, places of origin, etc…

It’s easy to see the has-a relationship and model a `NamedTuple` or `struct` this way, but is it correct to think of it that way?

1. If we go deeper, we could model our fruits this way `AbstractFruit` \> [`Apple`](https://bestapples.com/varieties-information/varieties/), in the [documentation it uses the word subtype](https://docs.julialang.org/en/v1/manual/types/#man-abstract-types), so `Apple` would be a subtype of `AbstractFruit`.

It’s easy to see the is-a relationship, so, is it correct to think of it that way?

I only ask this because, on the surface, it might be easy to explain that a variable name is like a box to store a value, and in languages such as Java and C# that might be true, but that thinking breaks down in languages like [Python and Julia](https://discourse.julialang.org/t/is-it-appropriate-to-say-a-variable-is-a-label-or-box/46068).

I’m not asking how to do [OOP in Julia](https://discourse.julialang.org/t/julia-oop-example/27568), but rather how do we take certain OO concepts and think about them the Julia way.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 11, 2021, 7:40pm UTC](https://discourse.julialang.org/t/vocabulary-when-using-namedtuples-and-structs/56960/2 "2021-03-11T19:40:06Z")

</div>

I cannot really reproduce your example there (I could not figure out how to use that `@NamedTuple` macro and what it solves, by the way, even reading the help).

But if you do:

```julia
julia> Fruit = @NamedTuple{name::String, type::String, color::String}
NamedTuple{(:name, :type, :color),Tuple{String,String,String}}

julia> x = (name="a",type="b",color="c")
(name = "a", type = "b", color = "c")

julia> typeof(x)
NamedTuple{(:name, :type, :color),Tuple{String,String,String}}

julia> x isa Fruit
true

```

Thus, I think we can say that `x` _is_ a `Fruit`, yes. But that `Fruit` is a concrete type, thus it does not have any subtype:

```julia
julia> subtypes(Fruit)
Type[]

```

So it is not exactly as if `Fruit` was an abstract concept below which things with different _sets_ of properties could exist. It would only make sense that calling the most general type that can characterize any fruit if all different fruit attributes where listed there.

One can explicitly use these concepts with structs:

```julia
julia> abstract type Fruit end

julia> struct Apple <: Fruit
         color 
       end

julia> struct Banana <: Fruit
          type
       end

julia> one_apple = Apple("Red")
Apple("Red")

julia> one_apple isa Apple
true

julia> one_apple isa Fruit
true

julia> one_banana = Banana("Nanica")
Banana("Nanica")

julia> one_banana isa Banana
true

julia> one_banana isa Fruit
true

# of course
julia> subtypes(Fruit)
2-element Array{Any,1}:
 Apple
 Banana

```

I do not see any harm in calling these things “objects”, for lack of words, but that does not completely fit into the object concept in programming, because objects can contain methods, and these “objects”, don’t.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 11, 2021, 9:53pm UTC](https://discourse.julialang.org/t/vocabulary-when-using-namedtuples-and-structs/56960/3 "2021-03-11T21:53:08Z")

</div>

Good question!

As you become more familiar with Julia, rather than “take certain OO concepts and think about them the Julian way” you are likely to consider aspects of Julia’s design, approach, and best practices as overlapping conceptual underpinnings of the Object Oriented perspective rather than seeking the deeper philosophical meld. That’s what I found most helpful.

Welcome to The Julia Community.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 12, 2021, 10:06am UTC](https://discourse.julialang.org/t/vocabulary-when-using-namedtuples-and-structs/56960/4 "2021-03-12T10:06:46Z")

</div>

> [@anon60034542](#):
>
> When you use a `NamedTuple` , is it correct to say that we have an object?

That really depends on how you want to think about it.

For example, one of the [OOP definitions](https://en.wikipedia.org/wiki/Object-oriented_programming) is that “objects contain data and code”, and since it is more typical to define _methods_ using multiple dispatch in Julia, which are not really “contained” in a value or a type, most things are not objects.

Or you can take the view that everything is an object. 😉

> [@anon60034542](#):
>
> how do we take certain OO concepts and think about them the Julia way.

Personally, I would sidestep/ignore most OO concepts and as I do not find them helpful for programming Julia.
