-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Method not found when shadowing function from base with a local variable #11723
Comments
Could you provide a reproducer? Maybe an issue with inner vs. outer constructors? |
I could boil it down to
Seems to have to do that the name |
I think I know what the problem is. Consider the following: type T
T() = new()
T(::Int) = T()
T(::String) = "I should not be here"
end
module M
const c = 1.0
const s = ""
const i = 1
global m = ""
end
import M: c,s,i,m
fc() = (global c = 1; T(c))
fs() = (global s = 1; T(s))
fi() = (global i = 1; T(i))
fm() = (global m = 1; T(m)) julia> fm()
Warning: imported binding for m overwritten in module Main
T()
julia> fi()
Warning: imported binding for i overwritten in module Main
T()
julia> fs()
Warning: imported binding for s overwritten in module Main
"I should not be here"
julia> fc()
Warning: imported binding for c overwritten in module Main
ERROR: MethodError: `convert` has no method matching convert(::Type{T}, ::Float64)
This may have arisen from a call to the constructor T(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert{T}(::Type{T}, ::T)
T()
...
in fc at none:1
The compiler doesn't realize that So yeah, the compiler needs to know when constants are overwritten. |
That's the point of constants: declaring you won't modify them to allow the compiler to write efficient code. It would be cool to have a mechanism to recompile the functions when the constant is modified, but AFAICT that's an expected behavior for now. But I don't see any use of constants in the original issue, so how is that related? |
But you're probably right. I was thinking for a second that maybe it would be useful to let users overwrite imported constants, but then I realized that no, it wouldn't :P Should there be an error for when the import being overwritten is a constant, as it causes this bug? Then again, most import overwrites are constants, almost forgot what I just said. Iunno. |
Also, I just tested, and constants are overwritten when globals with their names are defined, just their type information that remains unaccounted for: type T
x::Int
T(x::Int) = new(x)
T(x::FloatingPoint) = T(Int(x) + 10)
end
module Const
const m = 1.0
end
import Const.m
f() = (global m = 0; T(m)) julia>T(0)
T(0)
julia>T(1.0)
T(11)
julia> f()
Warning: imported binding for m overwritten in module Main
T(10)
|
So this is a bug. |
I suppose so. I think it makes sense to be able to overwrite imported constants, at least in REPL experimentation, but if that is not desired it should at least throw an error, as it does if one tries to modify a module-local constant with a value of different type, or at least warn about the risks of constant overwriting. By the way, did you manage to work around the problem? Changing the variable name solved it? |
I circumvented it. Could i get this tagged please, presumably with a bug tag, if you agree that it is a bug? |
Ping. |
Maybe @vtjnash is the right person to ping? |
Under master
|
@mschauer I saw you mentioned this issue on gitter. The issue here is that type-inference operates on the bindings that exist when type-inference is run. Type-inference adds a type-assert that the argument I don't think this is considered a bug; overwriting global bindings at the REPL is allowed for convenience, but that warning is intended to be meaningful as a warning. |
Thanks, that makes it very clear. |
I think this has never really been fixed, see #22525. |
I think this is fixed now, e.g.
|
I have the strange error
I don't really now where to start, but can this be a reasonable message at all? Julia knows and doesn't know about the inner constructor method
The text was updated successfully, but these errors were encountered: