Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require explicit import or module qualification when extending constructors #39890

Closed
nickrobinson251 opened this issue Mar 2, 2021 · 4 comments

Comments

@nickrobinson251
Copy link
Contributor

The behaviour for extending functions is:

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.

julia> (::String)(x) = println("boo!")

julia> "hello"(1)
boo!
@nickrobinson251 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
@vtjnash
Copy link
Member

vtjnash commented Mar 2, 2021

We may remove the former error, in v2 if not sooner. See #39235

@KristofferC
Copy link
Member

Dup of #25744

@nalimilan
Copy link
Member

Should we tag #25744 as 2.0 material not to forget it?

@KristofferC
Copy link
Member

Sure, why not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants