Skip to content

Commit

Permalink
Merge pull request #38 from JuliaObjects/only
Browse files Browse the repository at this point in the history
lens support for only
  • Loading branch information
jw3126 authored Jan 14, 2022
2 parents b93f054 + 0c82e52 commit 18ba733
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ using MacroTools
using MacroTools: isstructdef, splitstructdef, postwalk
using Requires: @require

include("setindex.jl")
include("optics.jl")
include("sugar.jl")
include("functionlenses.jl")
include("testing.jl")


if !isdefined(Base, :only)
Expand All @@ -22,4 +17,10 @@ function __init__()
@require StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" include("staticarrays.jl")
end

include("setindex.jl")
include("optics.jl")
include("sugar.jl")
include("functionlenses.jl")
include("testing.jl")

end
5 changes: 5 additions & 0 deletions src/functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ insert(obj, ::typeof(first), val) = insert(obj, IndexLens((firstindex(obj),)), v
set(obj, ::typeof(identity), val) = val
set(obj, ::typeof(inv), new_inv) = inv(new_inv)

function set(obj, ::typeof(only), val)
only(obj) # error check
set(obj, first, val)
end

################################################################################
##### eltype
################################################################################
Expand Down
6 changes: 6 additions & 0 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ using Accessors: test_getset_laws, test_modify_law
using Accessors: compose, get_update_op
using ConstructionBase: ConstructionBase
using StaticNumbers: StaticNumbers, static
if !isdefined(Base, :only)
using Accessors: only
end

struct T
a
Expand Down Expand Up @@ -126,6 +129,9 @@ end

t = (1, 2)
@test (@set t |> first = 10) === (10, 2)

@test @set(only((1,)) = 2 ) === (2,)
@test_throws ArgumentError @set(only((1,2)) = 2 )
end


Expand Down

0 comments on commit 18ba733

Please sign in to comment.