Skip to content

Commit 6d0d965

Browse files
authored
tweak NamedTuple operations to be more type-stable (#195)
1 parent bad0d00 commit 6d0d965

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/optics.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,10 @@ function (l::PropertyLens{field})(obj) where {field}
389389
getproperty(obj, field)
390390
end
391391

392-
@inline set(obj, l::PropertyLens{field}, val) where {field} = setproperties(obj, (; field => val))
392+
# Julia seems to prefers if types stay in the type domain the whole time like NamedTuple{(field,)}, instead of going through the value domain and constprop like (; field => val)
393+
@inline set(obj, l::PropertyLens{field}, val) where {field} = setproperties(obj, NamedTuple{(field,)}((val,)))
393394
@inline delete(obj::NamedTuple, l::PropertyLens{field}) where {field} = Base.structdiff(obj, NamedTuple{(field,)})
394-
@inline insert(obj::NamedTuple, l::PropertyLens{field}, val) where {field} = (; obj..., (;field => val)...)
395+
@inline insert(obj::NamedTuple{KS}, l::PropertyLens{field}, val) where {KS, field} = NamedTuple{(KS..., field)}((values(obj)..., val))
395396

396397
@inline set(obj::Tuple, l::PropertyLens{field}, val) where {field} = set(obj, IndexLens(field), val)
397398
@inline delete(obj::Tuple, l::PropertyLens{field}) where {field} = delete(obj, IndexLens(field))

0 commit comments

Comments
 (0)