# How to nest \`@auto\_hash\_equals\` & \`@with\_kw\`?

**URL:** <https://discourse.julialang.org/t/how-to-nest-auto-hash-equals-with-kw/33121>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 8, 2020, 10:08pm UTC](https://discourse.julialang.org/t/how-to-nest-auto-hash-equals-with-kw/33121 "2020-01-08T22:08:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [January 8, 2020, 10:08pm UTC](https://discourse.julialang.org/t/how-to-nest-auto-hash-equals-with-kw/33121/1 "2020-01-08T22:08:44Z")

</div>

How to nest the 2 macros? If I have both [`AutoHashEquals.@auto_hash_equals`](https://github.com/andrewcooke/AutoHashEquals.jl) & [`Parameters.@with_kw`](https://github.com/mauro3/Parameters.jl) applied onto a type definition. Either `@auto_hash_equals @with_kw` or `@with_kw @auto_hash_equals` is not working:

```julia
julia> @auto_hash_equals(@with_kw struct AAA
           a::String
           b
           c = [1, 1, 1]
       end)
ERROR: LoadError: AssertionError: typ.head == :type || typ.head == :struct
Stacktrace:
 [1] @auto_hash_equals(::LineNumberNode, ::Module, ::Any) at /Users/xxx/.julia/packages/AutoHashEquals/tDuUH/src/AutoHashEquals.jl:67
in expression starting at REPL[3]:1

```

```julia
julia> @with_kw(@auto_hash_equals struct AAA
           a::String
           b
           c = [1, 1, 1]
       end)
ERROR: LoadError: Only works on type-defs or named tuples.
Make sure to have a space after `@with_kw`, e.g. `@with_kw (a=1,)
Also, make sure to use a trailing comma for single-field NamedTuples.

Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] with_kw(::Expr, ::Module, ::Bool) at /Users/xxx/.julia/packages/Parameters/l76EM/src/Parameters.jl:307
 [3] @with_kw(::LineNumberNode, ::Module, ::Any) at /Users/xxx/.julia/packages/Parameters/l76EM/src/Parameters.jl:631
in expression starting at REPL[4]:100 

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 8, 2020, 11:58pm UTC](https://discourse.julialang.org/t/how-to-nest-auto-hash-equals-with-kw/33121/2 "2020-01-08T23:58:13Z")

</div>

This isn’t ideal, but because the transformations these macros do are somewhat orthogonal, you can just write the struct twice:

```julia
using Parameters, AutoHashEquals

@with_kw struct AAA
    a::String
    b
    c = [1, 1, 1]
end

@auto_hash_equals struct AAA
    a::String
    b
    c
end

julia> AAA(a = "hi", b = 1) == AAA("hi", 1, [1, 1, 1])
true

```

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [September 16, 2021, 7:12am UTC](https://discourse.julialang.org/t/how-to-nest-auto-hash-equals-with-kw/33121/3 "2021-09-16T07:12:09Z")

</div>

Found a package that solves this problem: [https://github.com/jw3126/StructHelpers.jl](https://github.com/jw3126/StructHelpers.jl)

```julia
struct S
    a
    b
end

@batteries S
@batteries S hash=false # don't overload `Base.hash`
@batteries S kwconstructor=true # add a keyword constructor

```
