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.


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:

  1. Intersection of two types:

    julia> typeintersect(Int, Number)

    In this example, the function returns the type Integer which represents the common elements between the Int and Number types.

  2. Intersection of arrays with different element types:

    julia> typeintersect([1, 2, 3], [1.0, 2.0, 3.0])

    The typeintersect function returns the Any type when there is no specific common type between the elements of the arrays.

  3. Intersection of custom types:
    julia> struct Dog end
    julia> struct Cat end
    julia> typeintersect(Dog, Cat)

    In this example, since there is no common parent type between Dog and Cat, the function returns Any.

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.

*Required Field

Checking you are not a robot: