You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
julia>using Base: run
julia>run(x) =1
ERROR: error in method definition:function Base.run must be explicitly imported to be extended
i.e. one must instead write Base.run(x) = 1 (or have used import Base: run)
However, the behaviour is different for constructors:
julia>using Base: Int
julia>Int(::String) =99
Int64
julia>Int("1")
99
I'd have expected this to error, just like the function extension case above, and for Base.Int(::String) = 99 to be required (or import).
I found this behaviour very surprising (given the behaviour for functions).
And ran into it in practice, where some private code was engaging in piracy that i'd expected to not be possible (thinking that importing names with using had protected against this, and not know the constructors had different behaviour).
Is this behaviour intended?
If not, is this something that would be practicable to change in v1.x? (I supposed we'd have to check with PkgEval to what extent this behaviour is relied upon in practice?)
This seems to have been the behaviour in all 1.x releases.
I think it would be preferable for the behaviour of constructors to be consistent with functions.
On Julia Slack, @MasonProtter points out you can also make types callable without qualification, e.g.
The text was updated successfully, but these errors were encountered:
nickrobinson251
changed the title
Require explicit import or module qualification when extending construcors
Require explicit import or module qualification when extending constructors
Mar 2, 2021
The behaviour for extending functions is:
i.e. one must instead write
Base.run(x) = 1
(or have usedimport Base: run
)However, the behaviour is different for constructors:
I'd have expected this to error, just like the function extension case above, and for
Base.Int(::String) = 99
to be required (orimport
).I found this behaviour very surprising (given the behaviour for functions).
And ran into it in practice, where some private code was engaging in piracy that i'd expected to not be possible (thinking that importing names with
using
had protected against this, and not know the constructors had different behaviour).Is this behaviour intended?
If not, is this something that would be practicable to change in v1.x? (I supposed we'd have to check with PkgEval to what extent this behaviour is relied upon in practice?)
This seems to have been the behaviour in all 1.x releases.
I think it would be preferable for the behaviour of constructors to be consistent with functions.
On Julia Slack, @MasonProtter points out you can also make types callable without qualification, e.g.
The text was updated successfully, but these errors were encountered: