what is the replacement for debug function in Julia? - julia

julia> debug("hello")
ERROR: UndefVarError: debug not defined
Stacktrace:
[1] top-level scope at REPL[6]:1
even tried with docker can't find suitable replacement.
help?> debug
search: #debug
Couldn't find debug
Perhaps you meant #debug, #enum, big or detach
No documentation found.
Binding debug does not exist.

Use the #debug macro to print debug output.
You have to enable debug output for you to actually see it. You can enable it with the env variable JULIA_DEBUG:
julia> #debug "hello"
julia> ENV["JULIA_DEBUG"] = "all"
"all"
julia> #debug "hello"
┌ Debug: hello
└ # Main REPL[4]:1

Related

Error defining a struct in Julia: syntax: invalid type signature around REPL[2]:1

I am trying to define a struct in Julia as follows:
julia> struct
myName::String
end
ERROR: syntax: invalid type signature around REPL[2]:1
Stacktrace:
[1] top-level scope
# REPL[2]:1
however I am getting the error shown above. Any suggestions to resolve this?
Silly me, the issue is the struct requires a name which I did not specific. I needed to do something like this:
julia> struct person
myName::String
end
which resolves the issue.

Models.Fitting type failure

I've inherited some old code, which was written for a much earlier version of Julia (~v0.6). I'm running Julia 1.5.4 and I've already adjusted a lot of the existing code, without much trouble.
But when I run:
import Models
type GLMA <: Models.Fitting.AbstractFittingAlgorithm
end
I get
ERROR: syntax: extra token "GLMA" after end of expression
Stacktrace:
[1] top-level scope at none:1
Why is this happening?
(a) Is there an issue with the type part?
Additionally, (b) Why is Models.Fitting missing when I do this:
Models.Fitting
ERROR: UndefVarError: Fitting not defined
Stacktrace:
[1] getproperty(::Module, ::Symbol) at .\Base.jl:26
[2] top-level scope at REPL[4]:1
I think you might have to declare it as an abstract type rather than a "bare" type. For example:
import Models
abstract type GLMA <: Models.Fitting.AbstractFittingAlgorithm; end
I don't see any bare type references, just abstract and primitive:
Types
Abstract Types
So with the following I was able to reproduce your error message:
julia> abstract type X end
julia> type Y <: X end
ERROR: syntax: extra token "Y" after end of expression
Stacktrace:
[1] top-level scope at none:1
A remote possibility is that the old code brought Fitting into scope with import or using and the keyword 'as' and if that renaming is not currently done, then you've lost that link. More here:
https://docs.julialang.org/en/v1/manual/modules/#Renaming-with-as

Delete a Method in Julia

I have a function with 2 methods
function foo(a::Integer) 42 end
function foo(a::String) 24 end
foo(2)
42
foo("a")
24
How can I delete just one of the two methods?
There is a type Method. Instances of that type refer to a specific method of a particular function.
particular_method = #which foo(2)
foo(a::Integer) in Main at /home/js/Documents/Julia/Matching/Query_Creation.jl:75
typeof(particular_method)
Method
And here's a way to delete the method using such an object:
Base.delete_method(particular_method)
foo(2)
ERROR: MethodError: no method matching foo(::Int64)
If you're writing your code in a file tracked by Revise, then deleting the method in the source will delete it in your running session. If I copy your two definitions to a file /tmp/delmeth.jl, then
julia> using Revise
julia> Revise.includet("/tmp/delmeth.jl")
julia> foo(2)
42
julia> foo("a")
24
Now, in your editor, delete the method for foo(::String), save the file, and in the same session do this:
julia> foo(2)
42
julia> foo("a")
ERROR: MethodError: no method matching foo(::String)
Closest candidates are:
foo(::Integer) at /tmp/delmeth.jl:1
Stacktrace:
[1] top-level scope
# REPL[6]:1
If you're developing anything "serious" & reusable, then you should generally create packages, in which case you don't need includet because Revise automatically tracks packages that you've loaded with using or import. See the Revise docs for further details.

Alternatives to parse and eval to check syntactic correctness for Julia v 1.1

Julia is dynamically typed and certain errors only occur during execution.
For instance:
julia> function foo()
a
5
end
foo (generic function with 1 method)
julia> foo()
ERROR: UndefVarError: a not defined
Stacktrace:
[1] foo() at ./REPL[1]:2
[2] top-level scope at none:0
julia>
Same behaviour using parse in combination with eval:
julia> eval(Meta.parse("function foo()
a
5
end"))
foo (generic function with 1 method)
However, when executing this an error is thrown:
julia> foo()
ERROR: UndefVarError: a not defined
Stacktrace:
[1] foo() at ./none:2
[2] top-level scope at none:0
Does it exist any standard facilities to check against these kinds of errors? Or does it exist any suitable packages for this task?
The VS Code Julia extension has an integrated linter that can detect usage of undefined variables, as in your example.
There is also this linter, Lint.jl, though I am not sure if it's up-to-date for Julia v1.x.

How do you check if a variable is defined inside a module in Julia?

isdefined(:x) will tell you if a variable x is defined in your current workspace.
If I want to check a variable is defined in a module (not one that's exported), how can I do that? I tried all of the following:
julia> module Test
x = 1
end
Test
julia> x
ERROR: UndefVarError: x not defined
julia> isdefined(:x)
false
julia> Test.x
1
julia> isdefined(:Test.x)
ERROR: type Symbol has no field x
julia> isdefined(:Test.:x)
ERROR: TypeError: getfield: expected Symbol, got QuoteNode
julia> isdefined(Test.:x)
ERROR: TypeError: getfield: expected Symbol, got QuoteNode
In the module Test above, I want to check if x is defined or not.
isdefined has an optional parameter for doing this. Try:
isdefined(Test, :x)
More information available through the usual channels: ?isdefined on the REPL and in the book: http://docs.julialang.org/en/release-0.4/stdlib/base/#Base.isdefined (link may be for older version, so the currently dominant search engine will help).
I think you need
:x in names(Test)
In Julia 1.1.0, isdefined's first parameter is not optional, instead there is a macro #isdefined(x) or #isdefined x that tests if x is defined. Calling it inside Test, checks if x is defined when in Test (inherited or not).
See documentation.

Resources