Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.

broadcast with arguments of different type #174

Closed
piever opened this issue Jan 24, 2017 · 4 comments
Closed

broadcast with arguments of different type #174

piever opened this issue Jan 24, 2017 · 4 comments

Comments

@piever
Copy link

piever commented Jan 24, 2017

First of all, kudos for #166, I find it extremely useful.
I have one doubt: by looking at the implementation, it seems that it only works if all the arguments of the function have the same type, i.e. this works fine:

NullableArray(ones(100)) .+ NullableArray(ones(100))

whereas this throws an error:

NullableArray(ones(Int64, 100)) .+ NullableArray(ones(Float64,100))

which as far as I understand comes from this definition of lift.

Is there a way/plan to generalize lift to tuples with elements of different types, or is that unfeasible?

@davidagold
Copy link
Contributor

Do you mean to say that NullableArray(ones(100)) .+ ones(100) throws an error?

@piever
Copy link
Author

piever commented Jan 24, 2017

Sorry! I copied the same code twice, I've edited the post above. Running this:

NullableArray(ones(Int64, 100)) .+ NullableArray(ones(Float64,100))

gives the following error:

ERROR: MethodError: no method matching lift(::Base.#+, ::Tuple{Nullable{Int64},Nullable{Float64}})

Of course this example is not useful in practice, but there are several use-cases where one would want to broadcast a function taking arguments of different types.

EDIT: of course you're right, I've changed it again, this is what give the lift related error message.

@davidagold
Copy link
Contributor

Well, the trouble there is that you have reversed the arguments to ones. But in general you seem to be right:

julia> NullableArray(ones(Int64, 100)) .+ NullableArray(ones(Float64, 100))
WARNING: Array{T,N}(::Type{T},d::NTuple{N,Int}) is deprecated, use Array{T,N}(d) instead.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:64
 [2] Array{T,N}(::Type{Float64}, ::Tuple{Int64}) at ./deprecated.jl:50
 [3] Type at /Users/David/.julia/v0.6/NullableArrays/src/constructors.jl:25 [inlined]
 [4] Type at /Users/David/.julia/v0.6/NullableArrays/src/constructors.jl:35 [inlined]
 [5] similar at ./abstractarray.jl:489 [inlined]
 [6] broadcast(::Base.#+, ::NullableArrays.NullableArray{Int64,1}, ::NullableArrays.NullableArray{Float64,1}) at /Users/David/.julia/v0.6/NullableArrays/src/broadcast.jl:68
 [7] eval(::Module, ::Any) at ./boot.jl:237
 [8] eval(::Module, ::Any) at /Users/David/julia/usr/lib/julia/sys.dylib:?
 [9] eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:66
 [10] macro expansion at ./REPL.jl:97 [inlined]
 [11] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:71
while loading no file, in expression starting on line 0
ERROR: MethodError: no method matching lift(::Base.#+, ::Tuple{Nullable{Int64},Nullable{Float64}})
Closest candidates are:
  lift(::Any) at /Users/David/.julia/v0.6/NullableArrays/src/lift.jl:37
  lift{F,N,T}(::F, ::Tuple{Vararg{T,N}}) at /Users/David/.julia/v0.6/NullableArrays/src/lift.jl:17
Stacktrace:
 [1] macro expansion at ./broadcast.jl:150 [inlined]
 [2] macro expansion at ./simdloop.jl:72 [inlined]
 [3] macro expansion at ./broadcast.jl:143 [inlined]
 [4] _broadcast! at ./broadcast.jl:136 [inlined]
 [5] broadcast_c! at ./broadcast.jl:207 [inlined]
 [6] broadcast! at ./broadcast.jl:201 [inlined]
 [7] invoke_broadcast!(::NullableArrays.#f2#17{Base.#+}, ::NullableArrays.NullableArray{Float64,1}, ::NullableArrays.NullableArray{Int64,1}, ::NullableArrays.NullableArray{Float64,1}) at /Users/David/.julia/v0.6/NullableArrays/src/broadcast.jl:41
 [8] broadcast(::Base.#+, ::NullableArrays.NullableArray{Int64,1}, ::NullableArrays.NullableArray{Float64,1}) at /Users/David/.julia/v0.6/NullableArrays/src/broadcast.jl:69

It should be possible to widen the argument signature for lift. Maybe we need to make the second argument type just Tuple and collect the relevant information from the parameters, which should be available.

@nalimilan
Copy link
Member

Good catch! It's surprising we didn't actually test this at all. #175 should fix it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants