This checks whether a specific promote rule exists:
promotionExists{T1, T2}( ::Type{T1}, ::Type{T2} ) = (Union{} !=
promote_rule(T1,T2))
On Tuesday, March 22, 2016 at 11:37:56 AM UTC-4, Bill Hart wrote:
>
> I'm having trouble understanding the following behaviour
> in 0.5.0-dev+3171. I wonder if someone can tell me what I'm doing wrong.
>
> module Mymod
> type mytype
> end
> end
>
> sig_table = [x.sig for x in methods(Base.promote_rule)]
>
> V = Tuple{typeof(Base.promote_rule),Type{Mymod.mytype},Type{Int64}}
>
> V in sig_table # returns true!!
>
> for s in sig_table # prints yes
> if V == s
> println("yes")
> end
> end
>
> for s in sig_table # prints nothing
> if s == V
> println("yes")
> end
> end
>
> Can someone explain what the difference between == and "in" is. For
> example, why shouldn't == be symmetric? And why should "in" tell me
> something is in an array that is clearly not in there?
>
> Metaquestion: what is the easiest way of checking if a promote_rule
> already exists? We have to create promote_rules at run time in response to
> user input (so it can't be done statically) and now the Julia compiler
> complains with pages of warnings because we are overwriting existing
> promote rules (actually, we are, harmlessly). We want to get rid of the
> warnings and the easiest way is to check if that promote rule already
> exists before defining it again.
>
> We can't just do method_exists because it always returns true for
> promote_rule, with any signature. So we need to check whether the promote
> rule with the precise signature we want to define already exists. For
> example
>
> method_exists(Base.promote_rule, Tuple{Type{Mymod.mytype}, Type{Int}})
>
> returns true.
>
> Bill.
>