Create a struct with keyward arguments

In Python, you can initialize a class with keyward arguments.

class A:
    def __init__(self, a, b):
d = {‘a’: 1, ‘b’: 2}
a = A(**d)

Is this possible in Julia?

You can use the Parameters.jl package:

julia> using Parameters

julia> @with_kw struct A
           a
           b
       end
A

julia> A(b = 2, a = 3)
A
  a: Int64 3
  b: Int64 2

julia> d = Dict(:b => 2, :a => 3);

julia> A(;d...)
A
  a: Int64 3
  b: Int64 2
1 Like