isa
isa(x, type) -> Bool
Determine whether x is of the given type.
Examples
-
Check if a value is of a specific type:
julia> x = 10; julia> isa(x, Int) trueThis example checks if the value
xis of typeInt. -
Verify type compatibility with abstract types:
julia> y = [1, 2, 3]; julia> isa(y, AbstractArray) trueIt confirms that
yis of typeAbstractArray, which includes all array-like types in Julia. - Check for subtype relationship with user-defined types:
julia> struct Person end; julia> struct Student <: Person end; julia> s = Student(); julia> isa(s, Person) trueIn this example,
isaverifies thatsis of typePerson, even though it is an instance of the subtypeStudent.
Common mistake example:
julia> a = 5;
julia> isa(a, Float64)
false
In this case, the mistake is expecting isa to return true for a being of type Float64. However, since a is an integer (Int), it is not of the requested type. It's essential to understand the type hierarchy and use isa appropriately to avoid such errors.
See Also
BigFloat, BigInt, Dict, eltype, fieldtype, Float32, Float64, IntSet, isa, isalnum, isalpha, isascii, iseltype, isequal, isgraph, isimmutable, isinteractive, isleaftype, isnull, ispunct, isspace, issubtype, keytype, Nullable, NullException, promote_type, typeintersect, typejoin, typemax, typemin, typeof, Val, valtype,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.