# Update on Base.@kwdef versus Parameters.jl

**URL:** <https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042>\
**Category:** General Usage\
**Tags:** parametersjl\
**Created:** [February 22, 2023, 7:57pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042 "2023-02-22T19:57:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [February 22, 2023, 7:57pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/1 "2023-02-22T19:57:39Z")

</div>

Hi,

I have checked the Stackoverflow topic [Difference between @with\_kw and Base.@kwdef in Julia?](https://stackoverflow.com/questions/69605856/difference-between-with-kw-and-base-kwdef-in-julia), whose last update is from Oct, 18, 2021, and would like to know whether there should be any drawbacks due to my current preferred use of the package `Parameters.jl` (with the corresponding macro `@with_kw`) instead of the default Julia’s `Base.@kw_def`; in fact, it seems to me the package still has (some/many?) more resources than the default base macro. Is this correct?

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 22, 2023, 9:26pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/2 "2023-02-22T21:26:33Z")

</div>

We have property destructuring in Base now, so `@unpack` isn’t needed:

```julia
(; x, y) = someobj

```

This combined with `@kwdef` and I’m not sure how much you get from Parameters.jl anymore.

`@kwdef` is exported now too:

> <https://github.com/JuliaLang/julia/blob/e3d366f1966595ba737220df49e220610823b331/base/exports.jl#L776>

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 22, 2023, 9:49pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/3 "2023-02-22T21:49:22Z")

</div>

I think that this syntax:

```julia
@with_kw mutable struct Settings @deftype Float64
    my_float = 2.0
end

```

still needs Parameters.jl

As far as I know Base.@kwdef does not support default types for the fields of a struct.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 22, 2023, 10:30pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/4 "2023-02-22T22:30:03Z")

</div>

> [@Raf](#):
>
> I’m not sure how much you get from Parameters.jl anymore

Compatibility with Julia 1.6? (for `@unpack`)

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 22, 2023, 10:59pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/5 "2023-02-22T22:59:48Z")

</div>

Sure, but this is a since 2021 update 😉

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 22, 2023, 11:23pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/6 "2023-02-22T23:23:47Z")

</div>

Yeah, but I keep using it because 1.6 is still the LTS.

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [February 24, 2023, 2:03pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/7 "2023-02-24T14:03:02Z")

</div>

Have the features mentioned by @mauro3 at the thread [at-kwdef support for parametric types and subtypes by simonbyrne · Pull Request #29316 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/29316#issuecomment-424091405) been merged to Base.@kwdef? I list them here for convenience as well:

> Four bits which are “missing” are:
> 
> - allow `@assert`s in the type body which then get included in the positional inner constructor.
> - different `show` method.
> - type-specific unpack macros
> - automatic documentation of the type-fields with their defaults
> 
> I don’t think the first three are suitable for Base. The last would be nice though, but maybe for another PR.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 24, 2023, 5:54pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/8 "2023-02-24T17:54:55Z")

</div>

> type-specific unpack macros

This is what I meant by property destructuring. Do you use the other things?

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [May 15, 2024, 10:09pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/9 "2024-05-15T22:09:04Z")

</div>

> - allow `@assert`s in the type body which then get included in the positional inner constructor.

Pulling on this thread a little - this one actually stops me converting by simple substitution from `@with_kw` to `@kwdef` because the structs are no longer syntactically valid.

```julia
julia> @kwdef struct Foo
         x::Int64
         @assert x > 3
       end
ERROR: UndefVarError: `x` not defined

```

Is there an easy fix for this? The assertions in the body are nice, I like how declarative it is.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 16, 2024, 2:00am UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/10 "2024-05-16T02:00:37Z")

</div>

That same error occurs without any `struct` macro, `struct` body `@assert`s are specifically [processed](https://github.com/mauro3/Parameters.jl/blob/501b47b468653b1f9c09b7af0732f9ce710ad330/src/Parameters.jl#L459) by Parameters.jl. It’s just a matter of making `@kwdef` do the same thing and having someone approve it. Not sure if it would be, as `struct` body `@assert`s are technically doable in base Julia, it’s just bizarre and seemingly useless to do an assertion with a global variable inside a `struct` body with an unrelated field of the same name.

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [May 16, 2024, 2:41pm UTC](https://discourse.julialang.org/t/update-on-base-kwdef-versus-parameters-jl/95042/11 "2024-05-16T14:41:23Z")

</div>

I see - I inherited this code and I didn’t realize `Parameters` was making the `@assert`s possible in the first place:

```julia
julia> struct Foo
           x::Int64
           @assert x > 3
       end
ERROR: UndefVarError: `x` not defined

```

How likely do you think adding this to base Julia would be? I assume that even if there were consensus on it, it would take a really long time.
