# Downsides to depending \`mutable\` field on \`DataType\`?

**URL:** <https://discourse.julialang.org/t/downsides-to-depending-mutable-field-on-datatype/27796>\
**Category:** General Usage\
**Created:** [August 21, 2019, 10:55am UTC](https://discourse.julialang.org/t/downsides-to-depending-mutable-field-on-datatype/27796 "2019-08-21T10:55:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cshenton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshenton/32/9326_2.png) [@cshenton](https://discourse.julialang.org/u/cshenton)\
**Post date:** [August 21, 2019, 10:55am UTC](https://discourse.julialang.org/t/downsides-to-depending-mutable-field-on-datatype/27796/1 "2019-08-21T10:55:01Z")

</div>

I need to check if a user defined type passed as an argument to a generated function is mutable or immutable (I’m doing a recursive vector of structs → struct of vectors transform, and I only want to split fields that are mutable all the way from the root type).

I’m aware `DataTypes` have a `mutable` field, and I suspect that’s not going anywhere. However this feels sort of brittle to me, and I wanted to check that I’d not be committing some huge wtf by defining:

```julia
abstract type Mutability end
struct Mutable <: Mutability end
struct Immutable <: Mutability end

Mutability(::Type{T}) where {T} = T.mutable ? Mutable() : Immutable()

```

---

<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:** [August 21, 2019, 11:26am UTC](https://discourse.julialang.org/t/downsides-to-depending-mutable-field-on-datatype/27796/2 "2019-08-21T11:26:33Z")

</div>

I am not sure I understand what you are trying to do, but there is `isimmutable` in `Base`, so you may not need traits.

---

<div class="post-metadata">

**Author:** ![cshenton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshenton/32/9326_2.png) [@cshenton](https://discourse.julialang.org/u/cshenton)\
**Post date:** [August 21, 2019, 11:31am UTC](https://discourse.julialang.org/t/downsides-to-depending-mutable-field-on-datatype/27796/3 "2019-08-21T11:31:04Z")

</div>

`isimmutable` takes a value, and I need to generate a type based on the mutability of another type. Is there a way to get `isimmutable` working with a type?

---

<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:** [August 21, 2019, 11:32am UTC](https://discourse.julialang.org/t/downsides-to-depending-mutable-field-on-datatype/27796/4 "2019-08-21T11:32:15Z")

</div>

If you have to operate on types, then accessing `T.mutable` is probably your best option.

But is hard to say more without context, can you make an MWE?

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [August 23, 2019, 3:46am UTC](https://discourse.julialang.org/t/downsides-to-depending-mutable-field-on-datatype/27796/5 "2019-08-23T03:46:50Z")

</div>

I don’t see any immediate reason to change this, but internal fields do move around sometimes. I’d take a PR to add an `isimmutabletype(T::DataType)` generic function that just returns that field. That’ll be easier to keep working than the raw field access.
