typesjulia

Finding out types of variables in Julia


In Python it is possible to debug a program by analyzing data types by printing out a variable's type, such as print type(test_var)

Is there something similar in Julia? I am having trouble assigning values to a 2-D array, and knowing the exact types of each variable would help.


Solution

  • You want typeof and possibly also isa:

    julia> a = 2
    2
    
    julia> typeof(a)
    Int64
    
    julia> typeof("haha")
    String
    
    julia> typeof(typeof("haha"))
    DataType
    
    julia> typeof(Set([1,3,4]))
    Set{Int64}
        
    julia> 1 isa Number
    true
    
    julia> 1 isa String
    false
    
    julia> "1" isa Number
    false
    
    julia> "1" isa String
    true
    

    You might also want to use @show as a convenient way to print debug info.