Module redefinition leads to inconsistent behaviour, at least when run in code blocks with Shift+Enter from vscode. Can anyone confirm this? I made a file and run it in cycle from top to bottom: #1, #2, THEN AGAIN #1:
#1
module Module1
mutable struct Foo
x::Int
end
end # module
using .Module1
foo(x::Module1.Foo) = x.x
x1 = Module1.Foo(1)
foo(x1)
#2
module Module1
mutable struct Foo
s::String
end
end # module
using .Module1
foo(x::Module1.Foo) = x.s
y1 = Module1.Foo("hi") # error
foo(y1)
At second run of #1 I get error:
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String
Closest candidates are:
convert(::Type{String}, ::String)
@ Base essentials.jl:298
convert(::Type{T}, ::T) where T<:AbstractString
@ Base strings\basic.jl:231
convert(::Type{T}, ::AbstractString) where T<:AbstractString
@ Base strings\basic.jl:232
...
Stacktrace:
[1] Main.Module1.Foo(s::Int64)
@ Main.Module1 c:\Users\gvg\YandexDisk\my_course\course-2023-1\wtf.jl:16
[2] top-level scope
@ c:\Users\gvg\YandexDisk\my_course\course-2023-1\wtf.jl:10
Meanwhile I never get the same error if just copy-paste these code blocks in the same order in julia REPL. What is going on here? What is the difference between running from vscode and REPL?
Interestingly, if I copy-paste #1 after #2 into #3 and run in cycle: #1, #2, #3, the problem disappears at some point. But I don’t understand the particular order, so it is just confusing to have the same error appearing and disappearing from time to time, depending on file line for the same code…
Tried this with Julia 1.9.0-rc1 and 1.8.5 on Windows 10 and 11, with vscode Julia plugin both release and pre-releave versions
UPDATE: Record