Skip to content

Commit

Permalink
move custom error hint for insert to a package extension
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonProtter committed Jul 26, 2024
1 parent 4c297d1 commit c6dec3a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand All @@ -21,13 +20,15 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"

[extensions]
AccessorsAxisKeysExt = "AxisKeys"
AccessorsIntervalSetsExt = "IntervalSets"
AccessorsStaticArraysExt = "StaticArrays"
AccessorsStructArraysExt = "StructArrays"
AccessorsUnitfulExt = "Unitful"
AccessorsMarkdownExt = "Markdown"

[compat]
AxisKeys = "0.2"
Expand Down
26 changes: 0 additions & 26 deletions src/Accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Accessors
using MacroTools
using MacroTools: isstructdef, splitstructdef, postwalk
using InverseFunctions
using Markdown: Markdown, @md_str, term

if !isdefined(Base, :get_extension)
using Requires
Expand All @@ -25,31 +24,6 @@ function __init__()
@static if !isdefined(Base, :get_extension)
@require StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" include("../ext/AccessorsStaticArraysExt.jl")
end
if isdefined(Base.Experimental, :register_error_hint)
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
if exc.f === insert && argtypes[2] <: Accessors.DynamicIndexLens
println(io)
term(io, md"""
`insert` with a `DynamicIndexLens` is not supported, this can happen when you write
code such as `@insert a[end] = 1` or `@insert a[begin] = 1` since `end` and `begin`
are functions of `a`. The reason we do not support these with `insert` is that
Accessors.jl tries to guarentee that `f(insert(obj, f, val)) == val`, but
`@insert a[end] = 1` and `@insert a[begin] = 1` will violate that invariant.
Instead, you can use `first` and `last` directly, e.g.
```
julia> a = (1, 2, 3, 4)
julia> @insert last(a) = 5
(1, 2, 3, 4, 5)
julia> @insert first(a) = 0
(0, 1, 2, 3, 4)
```
""")
end
end
end
end

end

0 comments on commit c6dec3a

Please sign in to comment.