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

map errors if function returns nothing #55

Open
bencottier opened this issue May 6, 2021 · 2 comments
Open

map errors if function returns nothing #55

bencottier opened this issue May 6, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@bencottier
Copy link

Bit of an edge case, but seems valid.

If using map on a KeyedDataset, and the block returns nothing, it errors, because of this line trying to assign nothing as a KeyedArray.

MWE:

julia> ds = KeyedDataset(:input => KeyedArray([1, 2]; a=[1, 2]));

julia> map(ds) do A
           println(typeof(A))
       end
KeyedArray{Int64, 1, NamedDimsArray{(:a,), Int64, 1, Vector{Int64}}, Base.RefValue{Vector{Int64}}}
ERROR: MethodError: Cannot `convert` an object of type 
  Nothing to an object of type 
  KeyedArray
Closest candidates are:
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/factorization.jl:58
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
  convert(::Type{T}, ::T) where T at essentials.jl:205
Stacktrace:
 [1] setindex!(dd::OrderedCollections.LittleDict{Tuple, KeyedArray, Vector{Tuple}, Vector{KeyedArray}}, value::Nothing, key::Tuple{Symbol})
   @ OrderedCollections ~/.julia/packages/OrderedCollections/cP9uu/src/little_dict.jl:219
 [2] map!(f::var"#9#10", dest::KeyedDataset, src::KeyedDataset)
   @ AxisSets ~/.julia/packages/AxisSets/IddYk/src/functions.jl:49
 [3] map(::Function, ::KeyedDataset)
   @ AxisSets ~/.julia/packages/AxisSets/IddYk/src/functions.jl:41
 [4] top-level scope
   @ REPL[16]:1

If the point of map(::KeyedDataset) is only to modify the dataset, that may be fair, but I think it should either have a fallback, or a clearer error.

@bencottier bencottier added the bug Something isn't working label May 6, 2021
@bencottier
Copy link
Author

More generally, there are convenient uses of map that don't return KeyedArray.

E.g. checking if any components are empty: any(map(isempty, ds)) rather than any(isempty.(values(ds.data))).

@rofinn
Copy link
Member

rofinn commented May 12, 2021

Yeah, I'll need to look more closely with how map figures out the return types for allocating containers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants