On August 26, 2015 at 21:30:47, Yichao Yu
([email protected](mailto:[email protected])) wrote:
> On Wed, Aug 26, 2015 at 12:44 PM, Sisyphuss wrote:
> > Of course, I know how to write the valid code.
> >
> > But in the interactive environment, if someone accidentally defines the
> > promote_rule in the non-base way, he will find himself in an
> > incomprehensible situation.
>
> There's too many things you can accidentally do to break normal
> functions in current scope and I don't think this one is particularly
> bad.
>
> ```
> julia> + = 1
> 1
>
> julia> 1 + 2
> ERROR: MethodError: `call` has no method matching call(::Int64,
> ::Int64, ::Int64)
> Closest candidates are:
> Union(::Any...)
> BoundsError(::Any...)
> TypeVar(::Any...)
> ...
> ```
Agreed, but if you reference the function being overwritten before assigning a
new definition, at least you get a warning:
```
julia> typeof(esc(+))
Expr
julia> + = 1
WARNING: imported binding for + overwritten in module Main
1
```
and in the original case:
```
julia> abstract B
julia> type A <: B end
julia> type C <: B end
julia> promote_rule
promote_rule (generic function with 124 methods)
julia> promote_rule(::Type{A}, ::Type{C}) = C
ERROR: error in method definition: function Base.promote_rule must be
explicitly imported to be extended
```
It would be nice if a user could get those warnings without having to reference
the function first.