eltype
eltype(type)
Determine the type of the elements generated by iterating a collection of the given type
. For associative collection types, this will be a Pair{KeyType,ValType}
. The definition eltype(x) = eltype(typeof(x))
is provided for convenience so that instances can be passed instead of types. However the form that accepts a type argument should be defined for new types.
Examples
julia> eltype([1, 2, 3])
Int64
In this example, the eltype
function is used to determine the type of elements generated by iterating over an array.
julia> eltype((1 => "one", 2 => "two"))
Pair{Int64, String}
Here, the eltype
function is applied to an associative collection (a dictionary) to determine the type of elements generated when iterating over it. The result is Pair{Int64, String}
.
julia> eltype("Hello, Julia!")
Char
In this case, the eltype
function is used to determine the type of elements generated by iterating over a string. The result is Char
.
julia> eltype([true, false, true])
Bool
Here, the eltype
function is used to determine the type of elements generated by iterating over an array of booleans. The result is Bool
.
Common mistake example:
julia> eltype(42)
ERROR: MethodError: no method matching eltype(::Int64)
In this example, the eltype
function is called with an integer value instead of a collection. The eltype
function is used to determine the type of elements generated by iterating a collection, so it cannot be directly applied to a non-collection value like an integer.
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.