Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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