# Create a struct with uninitialized fields

**URL:** <https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967>\
**Category:** General Usage\
**Tags:** struct, runtimegeneratedfunc\
**Created:** [November 8, 2017, 9:12pm UTC](https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967 "2017-11-08T21:12:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [November 8, 2017, 9:12pm UTC](https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967/1 "2017-11-08T21:12:00Z")

</div>

Having a mutable struct, e.g.:

```julia
mutable struct Point
    x
    y
end

```

is it possible to construct its instance with uninitialized fields and set them later? E.g.:

```julia
p = create_uninitialized(Point) # <-- anything like this?
p.x = 10.0
p.y = 20.0

```

Note: we can do it with arrays, e.g. `Array{Float64,1}(10)`.

* * *

Alternatively, I’d be happy to hear about `similar()` for structs, i.e. a function that takes an _instance_ of a struct and creates similar instance with dummy field values:

```julia
p = Point(8.0, 11.0)
p2 = similar_struct(p) # <-- like this
p2.x = 10.0
p2.y = 20.0

```

Currently I use:

```julia
p = Point(8.0, 10.0)
p2 = deepcopy(p)
p2.x = 10.0
p2.y = 20.0

```

But `deepcopy` is expensive and doesn’t work for all types.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 8, 2017, 9:15pm UTC](https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967/2 "2017-11-08T21:15:51Z")

</div>

Like this?

```julia
julia> mutable struct Point
           x
           y
           Point() = new()
       end

julia> p = Point()
Point(#undef, #undef)

julia> p.x = 2
2

julia> p.y = 3
3

julia> p
Point(2, 3)

```

Or do you want to do it without having access to the inner constructor?

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [November 8, 2017, 9:20pm UTC](https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967/3 "2017-11-08T21:20:21Z")

</div>

Yes, without any restrictions on a target type. I.e. user of this function should be able to use it for any mutable struct.

(However, thanks for the interesting trick - I didn’t know inner constructors may be used like this).

---

<div class="post-metadata">

**Author:** ![darsnack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darsnack/32/10144_2.png) [@darsnack](https://discourse.julialang.org/u/darsnack)\
**Post date:** [October 10, 2021, 1:52pm UTC](https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967/4 "2021-10-10T13:52:23Z")

</div>

What if I want to do this when I don’t have access to the inner constructor? Using Core is fine; the context here is I am trying to refactor a codebase that uses `ccall` to internal functions to be less fragile. So using Base functions would be best, but Core is still an improvement.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [October 10, 2021, 2:08pm UTC](https://discourse.julialang.org/t/create-a-struct-with-uninitialized-fields/6967/5 "2021-10-10T14:08:06Z")

</div>

I ended up with the following implementation:

```julia
"""
    __new__ (T, args...)
User-level version of the `new()` pseudofunction.
Can be used to construct most Julia types, including structs
without default constructors, closures, etc.
"""
@generated function __new__ (T, args...)
    return Expr(:splatnew, :T, :args)
end

```

I did’t try it with `undef` initializer, but given actual arguments, it works pretty well for quite a wide range if use cases in Ghost.jl. One caution though is that since `:new`/`:splatnew` are quite low-level, Julia won’t validate this expression, so misuse may lead to segfault.
