Update on Base.@kwdef versus Parameters.jl

Hi,

I have checked the Stackoverflow topic 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?

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

(; 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:

3 Likes

I think that this syntax:

@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.

Compatibility with Julia 1.6? (for @unpack)

Sure, but this is a since 2021 update :wink:

1 Like

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

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 been merged to Base.@kwdef? I list them here for convenience as well:

Four bits which are “missing” are:

  • allow @asserts 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.

type-specific unpack macros

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

  • allow @asserts 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> @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.

That same error occurs without any struct macro, struct body @asserts are specifically processed 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 @asserts 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.

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

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.