# Using a \`new\` constructor with Parameters.jl \`@with\_kw\`

**URL:** <https://discourse.julialang.org/t/using-a-new-constructor-with-parameters-jl-with-kw/84588>\
**Category:** Tooling\
**Tags:** question, package, parametersjl\
**Created:** [July 21, 2022, 12:28pm UTC](https://discourse.julialang.org/t/using-a-new-constructor-with-parameters-jl-with-kw/84588 "2022-07-21T12:28:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [July 21, 2022, 12:28pm UTC](https://discourse.julialang.org/t/using-a-new-constructor-with-parameters-jl-with-kw/84588/1 "2022-07-21T12:28:08Z")

</div>

I’d like to do something like this.

```nohighlight
using Parameters

struct Cat
    breed
    colour
    age
    weight
    name
end

@with_kw struct Pet
    name
    kind
    age
    function Pet(cat::Cat)
        new(name= cat.name, kind = typeof(cat), age = cat.age)
    end
end

fluffy = Cat("Fluffy", "White", 2, 10, "Fluffy")
pet = Pet(cat)

```

Here, there’s a constructor to easily make `Pet` instances from a `Cat` instance, but this gives the error

```nohighlight
ERROR: syntax: "new" does not accept keyword arguments around

```

How can I implement this correctly?

---

<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:** [July 21, 2022, 1:30pm UTC](https://discourse.julialang.org/t/using-a-new-constructor-with-parameters-jl-with-kw/84588/2 "2022-07-21T13:30:05Z")

</div>

```julia
julia> using Parameters

julia> struct Cat
           breed
           colour
           age
           weight
           name
       end

julia> @with_kw struct Pet
           name
           kind
           age
       end
Pet

julia> Pet(c::Cat) = Pet(name=c.name, kind=typeof(c), age=c.age)
Pet

julia> c = Cat("None", "Black", 5, 5.0, "John")
Cat("None", "Black", 5, 5.0, "John")

julia> Pet(c)
Pet
  name: String "John"
  kind: Cat <: Any
  age: Int64 5

```

You only need inner constructors if you want a `Pet(a,b,c)` constructor, which may be ambiguous with the default constructor.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [July 21, 2022, 1:33pm UTC](https://discourse.julialang.org/t/using-a-new-constructor-with-parameters-jl-with-kw/84588/3 "2022-07-21T13:33:59Z")

</div>

Thanks for your reply, while this is a sensible answer there’s two reasons why I’d rather not use this method:

1. I’d prefer for the constructor to be defined in the struct, it seems like a nicer syntax, and it is also more in keeping with a non-`Parameters.jl` approach
2. This method doesn’t work within Pluto notebooks, I appreciate I didn’t mention this in the initial question though  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/1/a193690fb4bf0d81f028cd93e23a39b2650ece32.png)

---

<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:** [July 21, 2022, 1:41pm UTC](https://discourse.julialang.org/t/using-a-new-constructor-with-parameters-jl-with-kw/84588/4 "2022-07-21T13:41:10Z")

</div>

> [@this\_josh](#):
>
> I’d prefer for the constructor to be defined in the struct, it seems like a nicer syntax

That is not the most common view. Inner constructions are the exception, not the rule.

> [@this\_josh](#):
>
> This method doesn’t work within Pluto notebooks,

You can just put them in a `begin... end` block, as suggested by the error message.

Anyway, both these are styling choices, you are not really limited by the functionality. But you can use an inner constructor without the keyword arguments, if you want:

```julia
julia> @with_kw struct Pet
           name
           kind
           age
           Pet(c::Cat) = new(c.name, typeof(c), c.age)
       end

julia> c = Cat("None", "Black", 5, 5.0, "John")
Cat("None", "Black", 5, 5.0, "John")

julia> Pet(c)
Pet
  name: String "John"
  kind: Cat <: Any
  age: Int64 5

```
