typeintersect
typeintersect(T, S)
Compute a type that contains the intersection of T and S. Usually this will be the smallest such type or one close to it.
Examples
In the Julia programming language, the function typeintersect(T, S) is used to compute a type that contains the intersection of T and S. It returns the smallest or a close approximation of the type that represents the common elements between T and S. Here are some examples of its usage:
-
Intersection of two types:
julia> typeintersect(Int, Number) IntegerIn this example, the function returns the type
Integerwhich represents the common elements between theIntandNumbertypes. -
Intersection of arrays with different element types:
julia> typeintersect([1, 2, 3], [1.0, 2.0, 3.0]) AnyThe
typeintersectfunction returns theAnytype when there is no specific common type between the elements of the arrays. - Intersection of custom types:
julia> struct Dog end julia> struct Cat end julia> typeintersect(Dog, Cat) AnyIn this example, since there is no common parent type between
DogandCat, the function returnsAny.
Common mistake example:
julia> typeintersect(1, "hello")
ERROR: MethodError: no method matching typeintersect(::Int64, ::String)
In this example, the function typeintersect is called with incompatible types Int64 and String. Make sure to provide compatible types to the function 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.