When I normally run tester.jl
below that calls my function encode(),
it fails with a stack trace. When I debug run it in VS Code, it runs fine and does not fail. I even get the answer I expect. I am using VScode 1.52.1 with Julia 1.5.3.
VScode info:
Version: 1.52.1 (user setup)
Commit: ea3859d4ba2f3e577a159bc91e3074c5d85c0523
Date: 2020-12-16T16:34:46.910Z
Electron: 9.3.5
Chrome: 83.0.4103.122
Node.js: 12.14.1
V8: 8.3.110.13-electron.0
OS: Windows_NT x64 10.0.18363
# run-length-encoding.jl
function encode(str)
# Run-length encoding (RLE)
rle = "";
n = length(str);
for i in 1:n
if i==1 # setup on the first iteration
c = str[1];
runLength = 1;
elseif c == str[i]
runLength +=1;
else
# Record current run
rle = runLength==1 ? string(rle, c) : string(rle, runLength, c);
# restart run
c = str[i];
runLength = 1;
end
# Handle the end of the string
if i==n
rle = runLength==1 ? string(rle, c) : string(rle, runLength, c);
end
end
return rle;
end
# tester.jl
include("run-length-encoding.jl");
encode("XYZ")
Stack Trace:
ERROR: LoadError: UndefVarError: c not defined
Stacktrace:
[1] encode(::String) at c:\Users\jason\Exercism\julia\run-length-encoding\run-length-encoding.jl:11
[2] top-level scope at c:\Users\jason\Exercism\julia\run-length-encoding\tester.jl:2
[3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1088
[4] include_string(::Module, ::String, ::String) at .\loading.jl:1096
[5] invokelatest(::Any, ::Any, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at .\essentials.jl:710
[6] invokelatest(::Any, ::Any, ::Vararg{Any,N} where N) at .\essentials.jl:709
[7] inlineeval(::Module, ::String, ::Int64, ::Int64, ::String; softscope::Bool) at c:\Users\jason\.vscode\extensions\julialang.language-julia-1.0.10\scripts\packages\VSCodeServer\src\eval.jl:185
[8] (::VSCodeServer.var"#61#65"{String,Int64,Int64,String,Module,Bool,VSCodeServer.ReplRunCodeRequestParams})() at c:\Users\jason\.vscode\extensions\julialang.language-julia-1.0.10\scripts\packages\VSCodeServer\src\eval.jl:144
[9] withpath(::VSCodeServer.var"#61#65"{String,Int64,Int64,String,Module,Bool,VSCodeServer.ReplRunCodeRequestParams}, ::String) at c:\Users\jason\.vscode\extensions\julialang.language-julia-1.0.10\scripts\packages\VSCodeServer\src\repl.jl:124
[10] (::VSCodeServer.var"#60#64"{String,Int64,Int64,String,Module,Bool,Bool,VSCodeServer.ReplRunCodeRequestParams})() at c:\Users\jason\.vscode\extensions\julialang.language-julia-1.0.10\scripts\packages\VSCodeServer\src\eval.jl:142
[11] hideprompt(::VSCodeServer.var"#60#64"{String,Int64,Int64,String,Module,Bool,Bool,VSCodeServer.ReplRunCodeRequestParams}) at c:\Users\jason\.vscode\extensions\julialang.language-julia-1.0.10\scripts\packages\VSCodeServer\src\repl.jl:36
[12] (::VSCodeServer.var"#59#63"{String,Int64,Int64,String,Module,Bool,Bool,VSCodeServer.ReplRunCodeRequestParams})() at c:\Users\jason\.vscode\extensions\julialang.language-julia-1.0.10\scripts\packages\VSCodeServer\src\eval.jl:110
[13] with_logstate(::Function, ::Any) at .\logging.jl:408
[14] with_logger at .\logging.jl:514 [inlined]
[15] (::VSCodeServer.var"#58#62"{VSCodeServer.ReplRunCodeRequestParams})() at c:\Users\jason\.vscode\extensions\julialang.language-julia-1.0.10\scripts\packages\VSCodeServer\src\eval.jl:109
[16] #invokelatest#1 at .\essentials.jl:710 [inlined]
[17] invokelatest(::Any) at .\essentials.jl:709
[18] macro expansion at c:\Users\jason\.vscode\extensions\julialang.language-julia-1.0.10\scripts\packages\VSCodeServer\src\eval.jl:27 [inlined]
[19] (::VSCodeServer.var"#56#57")() at .\task.jl:356
in expression starting at c:\Users\jason\Exercism\julia\run-length-encoding\tester.jl:2
My hunch is that c’s scope and definition are confined to the for-loop. In the debug run, c is defined just fine. In a normal run, c probably is optimized away, or somehow the for loop scope causes it not to be defined. Anyway, I think there is some bad practice here that I am violating but am a noob and still learning.
Thank you for explaining what is going on to a new Julia user.