Friday 15 January 2016

Julia: Check the type of variable

Julia provides typeof(), isa() functions to check the type of variables.

typeof(x)
Return the type of variable x.

julia> x="Hello";

julia> typeof(x)
ASCIIString

julia> x=123.456;

julia> typeof(x)
Float64

isa(x, type) -> Bool
Return true if x is of given type, else false.

julia> x="Hello";

julia> isa(x,ASCIIString)
true

julia> isa(x,Bool)
false


isa Vs Type hierarchy
Every type other than ‘Any’ has a parent type associated with it. You can get the parent type of any type using super(T) method.

julia> super(Int8)
Signed

julia> super(Signed)
Integer

julia> super(Integer)
Real

julia> super(Real)
Number

julia> super(Number)
Any

julia> super(Any)
Any


isa(x, type) method returns true, for all parent types of typeof(x).

julia> x=12345
12345

julia> typeof(x)
Int64

julia> isa(x, Int64)
true

julia> isa(x, Signed)
true

julia> isa(x, Integer)
true

julia> isa(x, Real)
true

julia> isa(x, Number)
true

julia> isa(x, Any)
true

julia> isa(x, Bool)
false


Note
Any is the super type for all data types in Julia.


Previous                                                 Next                                                 Home

No comments:

Post a Comment