Skip to content

Instantly share code, notes, and snippets.

@carlobaldassi
Created May 7, 2012 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlobaldassi/2630858 to your computer and use it in GitHub Desktop.
Save carlobaldassi/2630858 to your computer and use it in GitHub Desktop.
Union type dispatch bug
typealias MatOrNothing{T} Union(AbstractMatrix{T}, Vector{None}, Nothing)
my_func{T}(A::MatOrNothing{T}) = println("my_func ok")
M = [1. 2. ; 3. 4.]
my_func(M) # works
my_func([]) # works
my_func(nothing) # fails
typealias VecOrNothing{T} Union(AbstractVector{T}, Vector{None})
typealias MatOrNothing{T} Union(AbstractMatrix{T}, Vector{None})
my_func{T<:Real}(A::MatOrNothing{T}, B::MatOrNothing{T},
C::MatOrNothing{T}) = println("my_func ok")
M = [ 2. 1. ; 1. 1. ]
my_func(M, M, M)
#my_func([], M, M) # fails
my_func(M, [], M)
my_func(M, M, [])
my_func(M, [], [])
#my_func([], M, []) # fails
#my_func([], [], M) # fails
my_func([], [], [])
# like my_func but C is VecOrNothing : fails more often
my_func2{T<:Real}(A::MatOrNothing{T}, B::MatOrNothing{T},
C::VecOrNothing{T}) = println("my_func2 ok")
V = [ 0.; 0.]
my_func2(M, M, V)
#my_func2([], M, V) # fails
my_func2(M, [], V)
#my_func2(M, M, []) # fails
#my_func2(M, [], []) # fails
#my_func2([], M, []) # fails
#my_func2([], [], V) # fails
my_func2([], [], [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment