Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define error hint for insert on DynamicIndexLens. #140

Merged
merged 6 commits into from
Feb 28, 2024
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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Accessors"
uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
authors = ["Takafumi Arakaki <[email protected]>", "Jan Weidner <[email protected]> and contributors"]
version = "0.1.35"
version = "0.1.36"

[deps]
CompositionsBase = "a33af91c-f02d-484b-be07-31d278c5ca2b"
Expand All @@ -10,6 +10,7 @@ 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"
Copy link
Member

@aplavin aplavin Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I myself will use Accessors even with more deps, but not sure what others think.
Note that several existing deps can easily be turned into extensions, if not for the Julia ext precompilation bug. If that happens, only 4 actual deps would remain.

Personally, I think the (shortened) error message won't lose much without markdown.
Are there any existing markdown error hints in Julia?

But this is a minor point, fine either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW Markdown is a stdlib so it's not particularly costly or weird to rely on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can remove it too. I just like that it makes code snippets look a little distinct, and the REPL examples get syntax highlighting.

Copy link
Member

@aplavin aplavin Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe with this PR we should set the precedent for long and nicely formatted errorhints in Julia generally :) At least the Base ones are pretty minimal.

Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand All @@ -33,6 +34,7 @@ ConstructionBase = "1.5"
IntervalSets = "0.7"
InverseFunctions = "0.1.5"
MacroTools = "0.5"
Markdown = "1"
Requires = "0.5, 1.0"
StaticArrays = "1"
StructArrays = "0.6"
Expand Down
39 changes: 33 additions & 6 deletions src/Accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ module Accessors
using MacroTools
using MacroTools: isstructdef, splitstructdef, postwalk
using InverseFunctions

using Markdown: Markdown, @md_str, term

if !isdefined(Base, :get_extension)
using Requires
end
@static if !isdefined(Base, :get_extension)
function __init__()
@require StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" include("../ext/AccessorsStaticArraysExt.jl")
end
end


include("setindex.jl")
include("optics.jl")
Expand All @@ -25,4 +21,35 @@ include("../ext/AccessorsDatesExt.jl")
include("../ext/AccessorsLinearAlgebraExt.jl")
include("../ext/AccessorsTestExt.jl")

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
Loading