# Keep default constructor when adding a zero-argument constructor

**URL:** https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014
**Category:** New to Julia
**Created:** [August 26, 2019, 10:08pm UTC](https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014 "2019-08-26T22:08:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![milesf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milesf/32/9289_2.png) [@milesf](https://discourse.julialang.org/u/milesf)
#### Post date: [August 26, 2019, 10:08pm UTC](https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014/1 "2019-08-26T22:08:42Z")

</div>

If I add a zero-argument constructor `Foo()`, then I lose the ability to use the default constructor (e.g. `Foo(1, 2)`). This behavior is expected, and described in the [documentation](https://docs.julialang.org/en/v1/manual/constructors/index.html#Inner-Constructor-Methods-1): _If any inner constructor method is defined, no default constructor method is provided: it is presumed that you have supplied yourself with all the inner constructors you need._

If I want to keep the default constructor, I can create it by listing all arguments (or at least the correct number of arguments), but it seems like there should be a less-tedious way to maintain this part of the code. I tried using vargs `Foo(args...) = new(args)`, but calling `Foo(1, 2)` with that strategy produces `Foo((1, 2), #undef)`.

```julia
mutable struct Foo
  Foo() = new()
  #Foo(a, b) = new(a, b) # This works, but is not ideal
  #Foo(args...) = new(args) # Something like this would be better
  a
  b
end

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 26, 2019, 10:11pm UTC](https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014/2 "2019-08-26T22:11:08Z")

</div>

> [@milesf](#):
>
> If I add a zero-argument constructor `Foo()` , then I lose the ability to use the default constructor

Only if you add it as an inner constructor. Why can’t you add it as an outer constructor?

---

<div class="post-metadata">

### Author: ![milesf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milesf/32/9289_2.png) [@milesf](https://discourse.julialang.org/u/milesf)
#### Post date: [August 26, 2019, 10:19pm UTC](https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014/3 "2019-08-26T22:19:13Z")

</div>

Outer constructor does not have access to `new()`.  
So this doesn’t work:

```julia
mutable struct Foo
  a
  b
end
Foo() = new()

```

```julia
Foo()
ERROR: UndefVarError: new not defined

```

> An inner constructor method is like an outer constructor method, except for two differences:  
> 1. …  
> 2. It has access to a special locally existent function called new that creates objects of the block’s type.

> Outer constructor methods can only ever create a new instance by calling another constructor method, such as the automatically provided default ones.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 26, 2019, 10:27pm UTC](https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014/4 "2019-08-26T22:27:33Z")

</div>

So you want to be able to instantiate them with `undef` field values? OK.

```julia
Foo(args...) = new(args) # Something like this would be better

```

I guess you mean

```julia
Foo(args...) = new(args...)

```

That should work. The three dots after the input argument slurps them into a tuple. You have to splat them out again.

---

<div class="post-metadata">

### Author: ![martink](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@martink](https://discourse.julialang.org/u/martink)
#### Post date: [August 26, 2019, 10:44pm UTC](https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014/5 "2019-08-26T22:44:49Z")

</div>

> [@DNF](#):
>
> I guess you mean
> 
> ```julia
> Foo(args...) = new(args...)
> 
> ```

This appears to be not allowed (in Julia-1.1.0)

```julia
ERROR: syntax: ... is not supported inside "new"

```

---

<div class="post-metadata">

### Author: ![milesf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milesf/32/9289_2.png) [@milesf](https://discourse.julialang.org/u/milesf)
#### Post date: [August 26, 2019, 10:46pm UTC](https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014/6 "2019-08-26T22:46:01Z")

</div>

Lucky Julia release timing for me then. I’m testing on Julia-1.2.0

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [August 26, 2019, 10:50pm UTC](https://discourse.julialang.org/t/keep-default-constructor-when-adding-a-zero-argument-constructor/28014/7 "2019-08-26T22:50:45Z")

</div>

[https://github.com/JuliaLang/julia/pull/30577](https://github.com/JuliaLang/julia/pull/30577)
