Using a `new` constructor with Parameters.jl `@with_kw`

this_josh
Thu, 21 Jul 2022 12:28:08 +0000
Tooling

I’d like to do something like this.

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

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

How can I implement this correctly?

4 posts - 2 participants