# Default values for composite types in Julia

**URL:** https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479
**Category:** General Usage
**Tags:** question, proposal
**Created:** [June 27, 2017, 12:30am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479 "2017-06-27T00:30:29Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 27, 2017, 12:30am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/1 "2017-06-27T00:30:29Z")

</div>

In C++11, one can initialize the fields of a class to avoid boiler plate code. In Julia, I am currently typing every single combination of constructors in order to handle default values, for example:

```julia
immutable GaussianVariogram{T<:Real}
  sill::T
  range::T
  nugget::T
end
GaussianVariogram{T<:Real}(s::T, r::T) = GaussianVariogram(s, r, zero(T))
GaussianVariogram() = GaussianVariogram(1.,1.)

```

Is there a better mechanism for setting default values? Ideally, something like:

```julia
immutable GaussianVariogram{T<:Real}
  sill::T -> one(T)
  range::T -> one(T)
  nugget::T -> zero(T)
end

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 27, 2017, 12:32am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/2 "2017-06-27T00:32:00Z")

</div>

Look at Parameters.jl. It has a macro for exactly what you need.

> **[GitHub - mauro3/Parameters.jl: Types with default field values, keyword...](https://github.com/mauro3/Parameters.jl)**
>
> Types with default field values, keyword constructors and (un-)pack macros - GitHub - mauro3/Parameters.jl: Types with default field values, keyword constructors and (un-)pack macros

Sooner or later that will be part of Base in some form I think.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 27, 2017, 12:34am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/3 "2017-06-27T00:34:31Z")

</div>

On the spot @ChrisRackauckas, thanks. It would be great to have this as part of the syntax though, agree?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 27, 2017, 12:35am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/4 "2017-06-27T00:35:22Z")

</div>

Yes. But until someone makes that happen, this is not a bad solution.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 27, 2017, 12:47am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/5 "2017-06-27T00:47:39Z")

</div>

The solution doesn’t work with parameters though:

[https://github.com/mauro3/Parameters.jl/issues/32](https://github.com/mauro3/Parameters.jl/issues/32)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 27, 2017, 1:07am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/6 "2017-06-27T01:07:30Z")

</div>

Well, your example is fundamentally impossible.

```julia
immutable GaussianVariogram{T<:Real}
  sill::T -> one(T)
  range::T -> one(T)
  nugget::T -> zero(T)
end

```

You specify nowhere what `T` is, so how is it supposed to know? (In that setup, you want those all `UInt128`, right?) At least one default must determine `T`, otherwise there’s no way to know what it means.

But let’s say you do fix the example:

```julia
immutable GaussianVariogram{T<:Real}
  sill::T -> 1.0
  range::T -> one(T)
  nugget::T -> zero(T)
end

```

Anything that uses keyword arguments won’t dispatch, so if keyword arguments are used for the defaults it will not be type stable if you want to have a type parameter. Keyword argument dispatch will be a thing “soon”, so Parameters.jl will be able to handle that in the future, but for now you just have to live with it. That PR is really far along (it’s the one with `NamedTuples`) and it keeps getting mentioned, so I really think it will be 1.0 material. So I would just design with that in mind.

Default arguments are not a good option though because then the order matters. But if you want the default argument approach, then that’s easy to do by hand:

```julia
GaussianVariogram(sill=1.0,range=one(typeof(sill)),nugget=one(typeof(sill)) = GaussianVariogram(sill,range,nugget)

```

which is fine and type-inferable.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 27, 2017, 1:10am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/7 "2017-06-27T01:10:17Z")

</div>

Awesome @ChrisRackauckas, I am definitely sticking with `Parameters.jl`, and hoping it will be part of the language with additional syntax.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 27, 2017, 1:12am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/8 "2017-06-27T01:12:57Z")

</div>

For reference, here’s the Julialang issue which is discussing adding this:

[https://github.com/JuliaLang/julia/issues/10146](https://github.com/JuliaLang/julia/issues/10146)

Your point with wanting type parameters to work is definitely interesting and should be discussed.

---

<div class="post-metadata">

### Author: ![GDX64](https://avatars.discourse-cdn.com/v4/letter/g/c0e974/32.png) [@GDX64](https://discourse.julialang.org/u/GDX64)
#### Post date: [May 31, 2020, 2:56am UTC](https://discourse.julialang.org/t/default-values-for-composite-types-in-julia/4479/9 "2020-05-31T02:56:43Z")

</div>

Why don’t you put the default values on the constructor function? Like:

```julia
struct hello
    saludo::String
    function hello(arg = "oi")
        new(arg)
    end
end

```
