I don't see any caveats in `Setfield.jl / Accessors.jl`, but I'm getting a method

I don’t see any caveats in Setfield.jl / Accessors.jl, but I’m getting a method error (ERROR: LoadError: MethodError: no method matching) for the constructor that I’m trying to mutate. Are limitations outlined anywhere?

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

2 Likes

please post the line that errors

It took a bit to track down what the problem was. It seems that certain constructors must exist:

struct Foo{FT,A,B}
a::A
b::B
end

Foo{FT}(a::A,b::B) where {FT,A,B} = Foo{FT,A,B}(a,b)

f = Foo{Float64}(1, (;x=1,y=2))

using Accessors

# Fails:
@set f.b = (;x=2,y=1)

# Need this definition:
Foo(a,b) = Foo{typeof(a),typeof(a),typeof(b)}(a,b)

# Now works:
@set f.b = (;x=2,y=1)
1 Like

You can overload constructorof or setproperties. See here.

A PR that adds a HowTo to the Accessors.jl docs would be amazing!

1 Like