From 18f8f69d253219758ef9a0e77d74b71eaa6d1fc4 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 27 Feb 2024 15:16:38 +0100 Subject: [PATCH 1/6] define error hint for `insert` on `DynamicIndexLens`. --- Project.toml | 3 ++- src/Accessors.jl | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index b52a49e9..699ad5c3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Accessors" uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" authors = ["Takafumi Arakaki ", "Jan Weidner and contributors"] -version = "0.1.35" +version = "0.1.36" [deps] CompositionsBase = "a33af91c-f02d-484b-be07-31d278c5ca2b" @@ -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" Requires = "ae029012-a4dd-5104-9daa-d747884805df" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/Accessors.jl b/src/Accessors.jl index 2d201c8e..503df790 100644 --- a/src/Accessors.jl +++ b/src/Accessors.jl @@ -2,7 +2,7 @@ module Accessors using MacroTools using MacroTools: isstructdef, splitstructdef, postwalk using InverseFunctions - +using Markdown: Markdown, @md_str, term if !isdefined(Base, :get_extension) using Requires @@ -25,4 +25,32 @@ include("../ext/AccessorsDatesExt.jl") include("../ext/AccessorsLinearAlgebraExt.jl") include("../ext/AccessorsTestExt.jl") +function __init__() + 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 From 3751e191cfd091e9afd92600fca2f4814fc92ec7 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 27 Feb 2024 15:21:07 +0100 Subject: [PATCH 2/6] typo --- src/Accessors.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Accessors.jl b/src/Accessors.jl index 503df790..e386fd83 100644 --- a/src/Accessors.jl +++ b/src/Accessors.jl @@ -34,7 +34,7 @@ function __init__() `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 + Accessors.jl tries to guarantee 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. From 5b1523733f5f2849db0648d79918e9733bb25166 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 27 Feb 2024 15:58:49 +0100 Subject: [PATCH 3/6] compat bound on Markdown --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 699ad5c3..37a5f95a 100644 --- a/Project.toml +++ b/Project.toml @@ -34,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" From 7f78660835c005a03be12a5625da2cb3cf3fa536 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 27 Feb 2024 15:58:57 +0100 Subject: [PATCH 4/6] don't do `register_error_hint` on old versions --- src/Accessors.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Accessors.jl b/src/Accessors.jl index e386fd83..e2049321 100644 --- a/src/Accessors.jl +++ b/src/Accessors.jl @@ -26,7 +26,7 @@ include("../ext/AccessorsLinearAlgebraExt.jl") include("../ext/AccessorsTestExt.jl") function __init__() - if isdefined(Base.Experimental, :register_error_hint) + if VERSION >= 1.9 Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs if exc.f === insert && argtypes[2] <: Accessors.DynamicIndexLens println(io) From c53d31d312af7cd2dfac8e106b5987ae32d8a6a1 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 27 Feb 2024 16:04:07 +0100 Subject: [PATCH 5/6] oops --- src/Accessors.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Accessors.jl b/src/Accessors.jl index e2049321..f62c5d75 100644 --- a/src/Accessors.jl +++ b/src/Accessors.jl @@ -26,7 +26,7 @@ include("../ext/AccessorsLinearAlgebraExt.jl") include("../ext/AccessorsTestExt.jl") function __init__() - if VERSION >= 1.9 + if VERSION >= v"1.9" Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs if exc.f === insert && argtypes[2] <: Accessors.DynamicIndexLens println(io) From 5dec417c114671a590027fba8ee7df35a2f84ff9 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 27 Feb 2024 16:14:54 +0100 Subject: [PATCH 6/6] combine __init__s. --- src/Accessors.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Accessors.jl b/src/Accessors.jl index f62c5d75..2852fdea 100644 --- a/src/Accessors.jl +++ b/src/Accessors.jl @@ -7,11 +7,7 @@ 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") @@ -26,7 +22,10 @@ include("../ext/AccessorsLinearAlgebraExt.jl") include("../ext/AccessorsTestExt.jl") function __init__() - if VERSION >= v"1.9" + @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) @@ -34,7 +33,7 @@ function __init__() `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 guarantee that `f(insert(obj, f, val)) == val`, but + 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.