Accessors.jl icon indicating copy to clipboard operation
Accessors.jl copied to clipboard

Does not work with outer-only constructors

Open IlyaOrson opened this issue 4 years ago • 1 comments

Just wondering if this is a bug or a feature. Using the julia docs example:

julia> struct SummedArray{T<:Number,S<:Number}
           data::Vector{T}
           sum::S
           function SummedArray(a::Vector{T}) where T
               S = widen(T)
               new{T,S}(a, sum(S, a))
           end
       end

julia> sa = SummedArray([1,2,3])
SummedArray{Int64, Int128}([1, 2, 3], 6)

julia> @set sa.sum = 5
ERROR: MethodError: no method matching SummedArray(::Vector{Int64}, ::Int64)
Closest candidates are:
  SummedArray(::Vector{T}) where T at REPL[14]:4
Stacktrace:
 [1] setproperties_object(obj::SummedArray{Int64, Int128}, patch::NamedTuple{(:sum,), Tuple{Int64}})
   @ ConstructionBase C:\Users\ilyao\.julia\packages\ConstructionBase\N3vfl\src\ConstructionBase.jl:149
 [2] setproperties(obj::SummedArray{Int64, Int128}, patch::NamedTuple{(:sum,), Tuple{Int64}})
   @ ConstructionBase C:\Users\ilyao\.julia\packages\ConstructionBase\N3vfl\src\ConstructionBase.jl:63
 [3] set(obj::SummedArray{Int64, Int128}, l::Accessors.PropertyLens{:sum}, val::Int64)
   @ Accessors C:\Users\ilyao\.julia\packages\Accessors\KLYEx\src\optics.jl:376
 [4] top-level scope
   @ REPL[16]:1

IlyaOrson avatar Jan 18 '22 17:01 IlyaOrson

You can overload ConstructionBase.setproperties for types with inner constructors https://github.com/JuliaObjects/ConstructionBase.jl

jw3126 avatar Jan 18 '22 18:01 jw3126