# Transforming a type

**URL:** <https://discourse.julialang.org/t/transforming-a-type/56118>\
**Category:** General Usage\
**Tags:** type\
**Created:** [February 26, 2021, 9:04pm UTC](https://discourse.julialang.org/t/transforming-a-type/56118 "2021-02-26T21:04:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [February 26, 2021, 9:04pm UTC](https://discourse.julialang.org/t/transforming-a-type/56118/1 "2021-02-26T21:04:47Z")

</div>

I have a `Person` type and I want to make another type that’s similar but allows `missing` values in the fields. Is this possible?

```julia
struct Person
    name::String
    age::Int
end

struct IncompletePerson
    name::Union{String,Missing}
    age::Union{Int,Missing}
end

function allow_missing(t::DataType)::DataType

end

# @test allow_missing(Person).types == IncompletePerson.types

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 26, 2021, 9:12pm UTC](https://discourse.julialang.org/t/transforming-a-type/56118/2 "2021-02-26T21:12:13Z")

</div>

> [@jzr](#):
>
> ```julia
> function allow_missing(t::DataType)::DataType
> 
> end
> 
> ```

This should be possible, but you would need to use `eval` (to define the new type in global scope) after constructing the AST for the new type definition via introspection.

(You should probably view such a function as a convenience to save you from typing out the new definition, e.g. at module-loading time, but not something you would be normally be doing lots of at runtime.)

If you find yourself wanting to define lots of new types at runtime, you might consider using named tuples instead (which are basically anonymous `struct` types that can be constructed as needed).

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 26, 2021, 9:25pm UTC](https://discourse.julialang.org/t/transforming-a-type/56118/3 "2021-02-26T21:25:49Z")

</div>

Another possibility would be to define person as:

```julia
struct _Person{T}
    name::Union{String, T}
    age::Union{Int, T}
end

const Person = _Person{Union{}}

```

And then you can simply define the version that allows missing as:

```julia
const IncompletePerson = _Person{Missing}

```

That will only work if you define the struct yourself though, not if it comes from another package.
