Closed tkf closed 5 years ago
I realized that it's a bit tricky to define set
for a generic object. For tuples, it's easy:
struct ConstIndexLens{I} <: Lens end
Base.@propagate_inbounds Setfield.get(obj, ::ConstIndexLens{I}) where I = obj[I]
@generated function Setfield.set(obj::Tuple, ::ConstIndexLens{I}, val) where I
args = map(1:length(obj.types)) do n
n == I ? :val : :(obj[$n])
end
quote
($(args...),)
end
end
Maybe we can just fall-back to IndexLens
for non-tuples.
Indexing tuple with
IndexLens
is currently not type sable:How about adding (say)
ConstIndexLens
that stores the index in the type parameter? I think@lens _[$1]
would be a good sugar for this.