Skip to content

Conversation

@jondeuce
Copy link
Contributor

I ran into this issue while trying to use fmap_with_path while implementing an optimizer whose state is represented as a Matrix{NamedTuple}:

julia> fmap_with_path((k, x) -> (@show(k); x), [(a=1,) (b=2,)]);
ERROR: MethodError: no method matching KeyPath(::KeyPath{Tuple{}}, ::CartesianIndex{2})
The type `KeyPath` exists, but no method is defined for this combination of argument types when trying to construct it.

Closest candidates are:
  KeyPath(::Union{AbstractString, Integer, Symbol, KeyPath}...)
   @ Functors ~/.julia/dev/Functors/src/keypath.jl:60
  KeyPath(::T) where T<:Tuple
   @ Functors ~/.julia/dev/Functors/src/keypath.jl:55

Stacktrace:
  [1] (::Functors.var"#30#31"{KeyPath{Tuple{}}})(c::CartesianIndex{2})
    @ Functors ~/.julia/dev/Functors/src/walks.jl:80
  [2] iterate
    @ ./generator.jl:48 [inlined]
  [3] _collect
    @ ./array.jl:800 [inlined]
  [4] collect_similar
    @ ./array.jl:709 [inlined]
  [5] map
    @ ./abstractarray.jl:3371 [inlined]
  [6] _map
    @ ~/.julia/dev/Functors/src/walks.jl:3 [inlined]
  [7] DefaultWalkWithPath
    @ ~/.julia/dev/Functors/src/walks.jl:80 [inlined]
  [8] ExcludeWalkWithKeyPath
    @ ~/.julia/dev/Functors/src/walks.jl:135 [inlined]
  [9] (::Functors.CachedWalkWithPath{Functors.ExcludeWalkWithKeyPath{…}, Functors.NoKeyword, Functors.WalkCache{…}})(::Function, ::KeyPath{Tuple{}}, ::Matrix{NamedTuple{…} where names})
    @ Functors ~/.julia/dev/Functors/src/walks.jl:199
 [10] execute(walk::Functors.CachedWalkWithPath{Functors.ExcludeWalkWithKeyPath{…}, Functors.NoKeyword, Functors.WalkCache{…}}, x::KeyPath{Tuple{}}, ys::Matrix{NamedTuple{…} where names})
    @ Functors ~/.julia/dev/Functors/src/walks.jl:55
 [11] fmap_with_path(::Function, ::Matrix{NamedTuple{names, Tuple{Int64}} where names}; exclude::Function, walk::Functors.DefaultWalkWithPath, cache::IdDict{Any, Any}, prune::Functors.NoKeyword)
    @ Functors ~/.julia/dev/Functors/src/maps.jl:23
 [12] fmap_with_path(::Function, ::Matrix{NamedTuple{names, Tuple{Int64}} where names})
    @ Functors ~/.julia/dev/Functors/src/maps.jl:14
 [13] top-level scope
    @ REPL[2]:1
Some type information was truncated. Use `show(err)` to see complete types.

I can of course change the state representation, but I think this is a rather harmless extension to KeyPath. After this patch:

julia> fmap_with_path((k, x) -> (@show(k); x), [(a=1,) (b=2,)]);
k = KeyPath(CartesianIndex(1, 1), :a)
k = KeyPath(CartesianIndex(1, 2), :b)

Version info:

(jl_i3jhdk) pkg> st
Status `/tmp/jl_i3jhdk/Project.toml`
  [d9f16b24] Functors v0.5.1
julia> versioninfo()
Julia Version 1.11.1
Commit 8f5b7ca12ad (2024-10-16 10:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 32 × AMD Ryzen 9 3950X 16-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
Threads: 32 default, 0 interactive, 16 GC (on 32 virtual cores)

@CarloLucibello CarloLucibello merged commit 3771c34 into FluxML:master Nov 28, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants