terminaljulia

How can I check the type of a variable in Julia?


in Julia, im trying to check the type of a variable and i expected it to show both the type and the value like 'int 10'. but when i use typeof(x), it just shows the type, e.g., Int64. how can i display both the type and the value of a variable in the terminal?


Solution

  • You can do:

    x = 10
    println("$(typeof(x)) $x")
    

    or just:

    @show x
    

    which prints:

    x = 10
    

    (showing both the name, value, and from the REPL you'll still see the type if you want with typeof(x))

    If you want type + value in one string:

    println("$(typeof(x)) $(x)")
    

    Example output:

    Int64 10