Repository: JuliaDiff/FiniteDifferences.jl (julia)

Issue:
`to_vec` should make an empty vector for `CartesianIndex`
```julia
julia> to_vec(CartesianIndex(1))[1]
1-element Vector{Int64}:
 1
```
should be an empty vector instead

Pull request description:
Closes #196

Patch:
diff --git a/Project.toml b/Project.toml
index 21e4474..19ba357 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,6 +1,6 @@
 name = "FiniteDifferences"
 uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
-version = "0.12.19"
+version = "0.12.20"
 
 [deps]
 ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
diff --git a/src/to_vec.jl b/src/to_vec.jl
index e1916c0..66bb1f9 100644
--- a/src/to_vec.jl
+++ b/src/to_vec.jl
@@ -235,12 +235,15 @@ function to_vec(d::Dict)
     return d_vec, Dict_from_vec
 end
 
-# types
-function FiniteDifferences.to_vec(x::DataType)
-    function DataType_from_vec(x_vec::Vector)
-        return x
+# non-perturbable types
+for T in (:DataType, :CartesianIndex, :AbstractZero)
+    T_from_vec = Symbol(T, :_from_vec)
+    @eval function FiniteDifferences.to_vec(x::$T)
+        function $T_from_vec(x_vec::Vector)
+            return x
+        end
+        return Bool[], $T_from_vec
     end
-    return Bool[], DataType_from_vec
 end
 
 # ChainRulesCore Differentials
@@ -255,13 +258,6 @@ function FiniteDifferences.to_vec(x::Tangent{P}) where{P}
     return x_vec, Tangent_from_vec
 end
 
-function FiniteDifferences.to_vec(x::AbstractZero)
-    function AbstractZero_from_vec(x_vec::Vector)
-        return x
-    end
-    return Bool[], AbstractZero_from_vec
-end
-
 function FiniteDifferences.to_vec(t::Thunk)
     v, back = to_vec(unthunk(t))
     Thunk_from_vec = v -> @thunk(back(v))


Test patch:
diff --git a/test/to_vec.jl b/test/to_vec.jl
index 4e7a224..08f24bd 100644
--- a/test/to_vec.jl
+++ b/test/to_vec.jl
@@ -200,6 +200,13 @@ end
         test_to_vec(Vector; check_inferred=false) # isa UnionAll
     end
 
+    @testset "CartesianIndex" begin
+        test_to_vec(CartesianIndex(1))
+        test_to_vec(CartesianIndex(1, 2))
+        @test to_vec(CartesianIndex(1))[1] == []
+        @test to_vec(CartesianIndex(1, 3))[1] == []
+    end
+
     @testset "ChainRulesCore Differentials" begin
         @testset "Tangent{Tuple}" begin
             @testset "basic" begin