Le mardi 22 mars 2016 à 08:37 -0700, 'Bill Hart' via julia-users a
écrit :
> 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?
in() relies on isequal() for some collections like dicts. But that's
not the issue here, you're not even calling that function. It looks
like there's a bug with ==. Please file an issue on GitHub about it.
BTW, you can use === with types instead, and it seems to work fine
here.
Regards
> 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.