# Constructor with default values for mutables which allows undefined values

**URL:** https://discourse.julialang.org/t/constructor-with-default-values-for-mutables-which-allows-undefined-values/23290
**Category:** General Usage
**Created:** [April 19, 2019, 1:46am UTC](https://discourse.julialang.org/t/constructor-with-default-values-for-mutables-which-allows-undefined-values/23290 "2019-04-19T01:46:42Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [April 19, 2019, 1:46am UTC](https://discourse.julialang.org/t/constructor-with-default-values-for-mutables-which-allows-undefined-values/23290/1 "2019-04-19T01:46:42Z")

</div>

I’m looking for something like `Base.@kwdef` or `Parameters.@with_kw` but which leaves unspecified arguments for mutables as `#undef`, i.e. it should do,

```julia
@new_kwdef mutable struct Foo
   x = 1
   y
end

Foo() # should return Foo(1, #undef)
Foo(y=2) # should return Foo(1, 2)

```

Does anything like this already exist? Both of the aforementioned macros error on `Foo()` because `y` doesn’t have a default and isn’t defined.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [April 19, 2019, 2:12am UTC](https://discourse.julialang.org/t/constructor-with-default-values-for-mutables-which-allows-undefined-values/23290/2 "2019-04-19T02:12:35Z")

</div>

Oh, funnily enough I see now this was already asked about in [https://github.com/JuliaLang/julia/pull/27987](https://github.com/JuliaLang/julia/pull/27987), by me 🙂

I’m not sure if anyone has anything new to add to what is said there, fwiw I do still think it’s useful functionality.

---

<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: [April 19, 2019, 5:50am UTC](https://discourse.julialang.org/t/constructor-with-default-values-for-mutables-which-allows-undefined-values/23290/3 "2019-04-19T05:50:45Z")

</div>

> [@marius311](#):
>
> fwiw I do still think it’s useful functionality.

The comments made there are also relevant: it is special-casing mutable structs, and it is also at odds with the common behavior of functions just erroring when a keyword argument is not provided, eg

```julia
julia> f(; kw) = 1
julia> f()
ERROR: UndefKeywordError: keyword argument kw not assigned

```

That said, you can just write a macro for this, using `Base.@kwdef` as a starting point.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [April 19, 2019, 6:07am UTC](https://discourse.julialang.org/t/constructor-with-default-values-for-mutables-which-allows-undefined-values/23290/4 "2019-04-19T06:07:30Z")

</div>

> [@Tamas\_Papp](#):
>
> special-casing mutable structs

That argument I don’t totally get tbh, mutable structs are already special in that they can have `#undef` fields.

> [@Tamas\_Papp](#):
>
> That said, you can just write a macro for this, using `Base.@kwdef` as a starting point.

Certainly I can write a macro, although its not quite that easy because `@kwdef` creates an outer construction with keywords matching the struct fields, but its not as if I can give these keyword arguments a default value of `#undef` in the outer construction since that doesn’t exist in Julia.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [April 19, 2019, 6:11am UTC](https://discourse.julialang.org/t/constructor-with-default-values-for-mutables-which-allows-undefined-values/23290/5 "2019-04-19T06:11:24Z")

</div>

I guess the argument I can understand is that non-keyword outer constructors already can’t create `#undef` fields, e.g.

```julia
mutable struct Foo
    x
end
Foo() # this does not currently create F(#undef)

```

and so keyword constructors shouldn’t be able to either.

---

<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: [April 19, 2019, 6:24am UTC](https://discourse.julialang.org/t/constructor-with-default-values-for-mutables-which-allows-undefined-values/23290/6 "2019-04-19T06:24:47Z")

</div>

> [@marius311](#):
>
> mutable structs are already special in that they can have `#undef` fields.

So do (immutable) `struct`s, eg try

```julia
struct Foo
    x
    Foo() = new()
end
Foo()

```

(possible? yes; useful? not really.)

For your original issue: various extensions of constructors are possible, but IMO most of these should be addressed (and experimented with) in packages, not `Base`.
