# If I have the following struct:\`\`\`mutable struct FactorF{V, C, T} # variables, card

**URL:** <https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611>\
**Category:** General Usage\
**Created:** [March 6, 2021, 11:32am UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611 "2021-03-06T11:32:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 6, 2021, 11:32am UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611/1 "2021-03-06T11:32:44Z")

</div>

If I have the following struct:

```julia
mutable struct FactorF{V,C,T} # variables, cardinality, array type
  vals::T
end

```

and I have an instance of `Factor` where `T` happens to be `Array{Float64,3}`, what is the proper way to access the `Float64` ? Here is an example of an approach I found:

```julia
import Base: eltype
eltype(::FactorF{V,C,T}) where {V,C,T} = T.parameters[1]

A = FactorF{(1,2,3), (3,3,3), Array{Float64,3}}(rand(3,3,3))
eltype(A) |&gt; println # prints Float64

```

Is this the proper way to do this?

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1615026985468900?thread_ts=1615026985.468900&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [March 6, 2021, 1:33pm UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611/2 "2021-03-06T13:33:27Z")

</div>

I’d like

```julia
eltype(f::FactorF) = eltype(f.vals)

```

---

<div class="post-metadata">

**Author:** ![mroavi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mroavi/32/8804_2.png) [@mroavi](https://discourse.julialang.org/u/mroavi)\
**Post date:** [March 6, 2021, 3:37pm UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611/3 "2021-03-06T15:37:36Z")

</div>

Thanks @liuyxpp. The definition you propose depends on information stored in a field rather than in the type itself. So I presume that the performance won’t be as good.

@abraunst proposed this other definition in slack:

```julia
eltype(::FactorF{V,C,<:AbstractArray{T}}) where {V,C,T} = T

```

which only depends on info stored in the type. I think I’m gonna go with this one.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [March 7, 2021, 1:38am UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611/4 "2021-03-07T01:38:27Z")

</div>

I am not sure the performance. But it is not as flexible as eltype(field) approach as the eltype is achieved in a recursive way. Thus if `T` in your definition is another `AbstractArray`, then the approach proposed above will only return this `AbstractArray` type rather than its element type.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [March 7, 2021, 7:45am UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611/5 "2021-03-07T07:45:04Z")

</div>

> [@mroavi](#):
>
> The definition you propose depends on information stored in a field rather than in the type itself. So I presume that the performance won’t be as good.

Not necessarily. The compiler often propagates constants in such cases, so it won’t even use the value itself.  
Also, `eltype` can be called on type itself, so you can define

```julia
eltype(::FactorF{V,C,T}) where {V,C,T} = eltype(T)

```

`T.parameters[1]` is certainly not a good choice, because e.g for static arrays the first parameter is size, not the element type.

---

<div class="post-metadata">

**Author:** ![mroavi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mroavi/32/8804_2.png) [@mroavi](https://discourse.julialang.org/u/mroavi)\
**Post date:** [March 7, 2021, 10:58am UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611/6 "2021-03-07T10:58:57Z")

</div>

Thanks for the insight!

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [March 11, 2021, 2:17pm UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611/7 "2021-03-11T14:17:06Z")

</div>

This doesn’t seem to be true

```julia
julia> x=[[1 2; 3 4] for i=1:2, j=1:2]
2×2 Matrix{Matrix{Int64}}:
 [1 2; 3 4] [1 2; 3 4]
 [1 2; 3 4] [1 2; 3 4]
julia> eltype(x)
Matrix{Int64} (alias for Array{Int64, 2})

```

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [March 12, 2021, 1:25am UTC](https://discourse.julialang.org/t/if-i-have-the-following-struct-mutable-struct-factorf-v-c-t-variables-card/56611/8 "2021-03-12T01:25:08Z")

</div>

Nice catch! It seems to depend on the implementation of `eltype` of the next level type.
